Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Configuration Class is simply ignored and not loaded

I have the following @Configuration class on the classpath of a few of my @SpringBootApplications:

@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?

like image 411
filpa Avatar asked Dec 06 '16 19:12

filpa


People also ask

What does @configuration do in spring boot?

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.

How do you call a configuration class in spring boot?

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.

How do I auto-configure in spring boot?

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.

Can we use @bean without @configuration?

@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.


1 Answers

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.

like image 130
Tom Avatar answered Sep 23 '22 14:09

Tom