I'm trying to create a MainConfig that imports another Config by using an @Bean method instead of @Import like this :
@Configuration
public class MainConfig {
@Bean
public Service service() {
return new Service(infrastructureConfig().database());
}
@Bean
public OtherService otherService() {
return new OtherService(infrastructureConfig().database());
}
@Bean
public InfrastructureConfig intrastructureConfig() {
return new InfrastructureConfig();
}
}
@Configuration
public class InfrastructureConfig {
@Bean
public Database database() {
return new Database();
}
...
}
When using this technique, the Database is created twice because Spring doesn't seem to consider the @Configuration annotation on InfrastructureConfig. When using @Import, it works fine.
I don't want to use @Import because I want to mock my InfrastructureConfig like this :
@Configuration
public class TestConfig extends MainConfig {
@Override
public InfrastructureConfig infrastructureConfig() {
return mock(InfrastructureConfig.class);
}
}
Am I missing something or it is not supported ?
Thanks
I opened a Spring ticket SpringSource JIRA and they said that it is a known limitation and it is working as designed.