conditionally enabled bean - null when disabled
@Component @ConditionalOnProperty(prefix = "myApplication.features", name = "feature1", havingValue="true") public class Feature1 {
If the conditional bean is a controller, you will not need to auto-install it, since the controller is usually not entered. If a conditional bean is entered, you will get No qualifying bean of type [xxx.Feature1] when it is not enabled, so you need to auto-install it using required=false . Then it will remain null .
Conditional Enabled and Disabled beans
If Feature1 bean is introduced into other components, you can enter it using required=false or define a bean to return when the function is disabled:
@Component @ConditionalOnProperty(prefix = "myApplication.features", name = "feature1", havingValue="true") public class EnabledFeature1 implements Feature1{
Conditional Enabled and Disabled beans - Spring Configuration :
@Configuration public class Feature1Configuration{ @Bean @ConditionalOnProperty(prefix = "myApplication.features", name = "feature1", havingValue="true") public Feature1 enabledFeature1(){ return new EnabledFeature1(); } @Bean @ConditionalOnProperty(prefix = "myApplication.features", name = "feature1", havingValue="false") public Feature1 disabledFeature1(){ return new DisabledFeature1(); } } @Autowired private Feature1 feature1;
Spring profiles
Another option is to activate beans profiles through Spring: @Profile("feature1") . However, all included functions should be listed in one property spring.profiles.active=feature1, feature2... , so I believe that this is not what you want.
source share