I have the following @Configuration
class on the classpath of a few of my @SpringBootApplication
s:
@Configuration @Import({MainConfig.class, RestConfig.class}) public class ApiConfig { @Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public Client client() throws ExecutionException, InterruptedException { return service.create(Client.class); } }
I have two services that use this config (with differently named Client
classes).
Service 1 starts correctly and loads this config. I can see during start up that a bean of type ApiConfig
was eagerly initialized.
Service 2 starts incorrectly: the above configuration class is simply ignored and not initialized.
The services are started in separate JVMs.
Ther services have nearly identical, very small application.properties
files:
spring.application.name=xxx-api server.port=0 eureka.name=xxx.api # Only for reading properties from a central location context.initializer.classes=com.package.contextClass
I'm not even sure what kind of additional information I could write into the question. I have been going through logs for a couple of hours now and see no discernible difference, simply that it plainly ignores my @Configuration
class.
Has anyone had this issue before?
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
Spring @Configuration annotation usageUse @Configuration annotation on top of any class to declare that this class provides one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
Auto-Configuration in Spring BootThe annotation @EnableAutoConfiguration is used to enable the auto-configuration feature. The @EnableAutoConfiguration annotation enables the auto-configuration of Spring ApplicationContext by scanning the classpath components and registering the beans.
@Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.
The @SpringBootApplication
annotation (or, more precisely the inferred @ComponentScan
annotation) by default only scans the classpath next to and below the annotated class.
So, your configuration class must be placed next to or in a sub package of you Application class.
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