Profiling configuration overridden by default configuration
Hello, I have an issue running the spring boot with different profiles that should override beans.
Versions:
- Java 1.8.0_60
- Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T14:57:37+03:00)
Project has the following architecture:
src/main/java/
Application.java
config/ApplicationConfiguration.java
config/LocalConfiguration.java
service/SomeService.java
service/SomeServiceImpl.java
service/SomeLocalServiceImpl.java
src/main/resources
application.properties
application-local.properties
pom.xml
spring-boot version:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
</parent>
The classes definition look as:
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private SomeService someService;
@Override
public void run(String... args) {
// someService method call
}
public static void main(String[] args) {
SpringApplication application = new SpringApplication(AllocationApplication.class);
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
SpringApplication.run(AllocationApplication.class, args);
}
}
@Configuration
public class ApplicationConfiguration{
@Bean
public SomeService someService(){
return new SomeServiceImpl();
}
}
@Profile("local")
@Configuration
public class LocalConfiguration{
@Bean
public SomeService someService(){
return new SomeLocalServiceImpl();
}
}
When running the application with --spring.profiles.active=local I notice that the LOCAL beans are overridden by default ones:
2015-10-28 11:37:52.137 INFO [main] f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'someService': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=localConfiguration; factoryMethodName=someService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [config/LocalConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=applicationConfiguration; factoryMethodName=someService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [config/ApplicationConfiguration.class]]
OK found the solution however for me it's weird, by applying the annotation @Profile("!local") on the bean definition method fixed it.
@Configuration
public class ApplicationConfiguration{
@Bean
@Profile("!local")
public SomeService someService(){
return new SomeServiceImpl();
}
}
For me is still unclear why the default configuration override the local if that one is active. Basically my understanding is that first is default, then is the active that overrides.
Please correct me if I'm wrong. Thanks.
Your @Profile("local") annotation is just telling Spring Framework to enable LocalConfiguration when the local profile is active. You haven't specified anywhere that ApplicationConfiguration should be disabled when the local profile is active. As you've observed adding @Profile("!local") does that.
An alternative would be to order your configuration classes and make sure that LocalConfiguration is processed after ApplicationConfiguration. That would allow LocalConfiguration's definition of someService to override the definition from ApplicationConfiguration.