I'm using Spring Boot 2.1.6.RELEASE and I'm wondering how @ConditionalOnMissingBean
should be used?
Example configuration:
@Configuration
public class MyAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Foo foo() {
return new Foo();
}
}
@Configuration
@Import(MyAutoConfiguration.class)
public class AppConfiguration {
@Bean
public Foo foo() {
return new Foo("some property");
}
}
AppConfiguration.class
and MyAutoConfiguration.class
are in different modules.
Error:
The bean 'foo', defined in class path resource [com/example/autoconf/configuration/AppConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/my/configuration/MyAutoConfiguration.class] and overriding is disabled.
How to use @ConditionalOnMissingBean
without setting spring.main.allow-bean-definition-overriding=true
property?
For MyAutoConfiguration
to be picked up by Spring Boot as auto-configuration it need to be declared in META-INF/spring.factories
as below
org.springframework.boot.autoconfigure.EnableAutoConfiguration=x.y.z.MyAutoConfiguration
For more details related to custom auto-configurations have a look here.
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