If I have a application.yml that looks like
system:
props:
version: 1.0.0
host: myhost
myapp:
environment: LOCAL
---
spring:
profiles: DEV
system:
myapp:
environment: DEV
devhost: mydevhost
---
spring:
profiles: PROD
system:
myapp:
environment: PROD
testhost: myprodhost
And I set up a config class using
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class MyConfig {
private string devhost;
:
:
public String getDevHost() {return devhost;}
}
Am I able to autowire MyConfig into a class that is an annotated bean of a class that looks like
@Configuration
@EnableJms
public class MyOtherConfig {
@Bean
public Object beanThatNeedsMyConfig(){return new beanThatNeedsMyConfig();}
}
I tried using my intuition and Autowired it in beanThatNeedsMyConfig() and I'm calling on it by saying myConfig.getDevHost(), but I'm getting a null pointer as myConfig is null. I'm assuming Spring has yet to acknowledge it's existence. I'm still working on grasping Spring Boot and lifecycles, etc, etc. Any knowledge on how I can accomplish this?
So, what is happening in your original code is that you're not autowiring anything into your bean that needs configuration. When you do this:
@Bean
public Object beanThatNeedsMyConfig(){return new beanThatNeedsMyConfig();}}
You're not passing the MyConfig bean into the bean that needs it. Autowiring is wonderful, but its not magick. To pass it in you should autowire it into the MyOtherConfig class, or you should remove the beanThatNeedsMyConfig() method completely which would allow spring to create the bean and do the autowiring automagically.
Btw: The @Inject annotation should really only be used on constructors, any anyone who tells you differently is lying to you. If you had it on the constructor for BeanThatNeedsMyConfig, you'd have not been able to construct it without passing the object in and your problem would have been self evident.
Review the Spring Boot docs(structure and autoconfiguration of beans).
In general, it's easiest to put your SpringBootApplication at the root of your package hierachy (e.g. com.example.myapp), then extend as needed (e.g. com.example.myapp.model). If you do this, you don't have to do anything other than annotate your Application with SpringBootApplication for a basic application. You may need other annotations for additional features, like EnableJpaRepositories.
If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan without any arguments. All of your application components (@Component, @Service, @Repository, @Controller etc.) will be automatically registered as Spring Beans.
'Automatically registered as Spring Beans' means you don't need to define an @Bean method that uses the default or an Autowired constructor. These are automatically provided for you. You only need to explicitly define @Bean methods for things you need to configure manually, such as something from a custom Builder object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With