Switch Function for Spring Components

I have a Spring boot application with a lot of commented components @Component, @Controller, @RestController. There are about 20 different functions that I would like to switch separately. It is important that the functions can be switched without restoring the project (a reboot will be in order). I think Spring configuration would be a nice way.

I could create a configuration (yml) as follows:

myApplication: features: feature1: true feature2: false featureX: ... 

The main problem is that I do not want to use if blocks in all places. I would prefer to completely disable the components. For example, @RestController should even be loaded, and it should not register its corrections. I'm currently looking for something like this:

 @Component @EnabledIf("myApplication.features.feature1") // <- something like this public class Feature1{ // ... } 

Is there such a function? Is there an easy way to implement it myself? Or is there another best practice for switching functions?

Btw: Spring Boot Version: 1.3.4

+5
source share
4 answers

You can use @ConditionalOnProperty annotation:

 @Component @ConditionalOnProperty(prefix = "myApplication.features", name = "feature1") public class Feature1{ // ... } 
+7
source

conditionally enabled bean - null when disabled

 @Component @ConditionalOnProperty(prefix = "myApplication.features", name = "feature1", havingValue="true") public class Feature1 { //... } @Autowired(required=false) private Feature1 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{ //... } @Component @ConditionalOnProperty(prefix = "myApplication.features", name = "feature1", havingValue="false") public class DisabledFeature1 implements Feature1{ //... } @Autowired private Feature1 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.

+5
source

Try to see ConditionalOnExpression

and maybe this should work

 @Component @ConditionalOnExpression("${myApplication.controller.feature1:false}") // <- something like this public class Feature1{ // ... } 
+1
source

FF4J is an environment for implementing a function switching pattern. It offers a spring-boot starter and allows you to enable or disable spring components at run time through a dedicated web console. Using using AOP , it allows you to dynamically inject the correct beans based on the state of the function. It does not add or remove a bean from the spring context.

+1
source

All Articles