Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feature-Toggle for Spring components

I have a Spring Boot application with a lot of @Component, @Controller, @RestController annotated components. There are about 20 different features which I would like to toggle separatly. It is important that features can be toggled without rebuilding the project (a restart would be ok). I think Spring configuration would be a nice way.

I could image a config (yml) like this:

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

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

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

Is there a feature like this? Is there an easy way to implement it myself? Or is there another best practice for feature toggles?

Btw: Spring Boot Version: 1.3.4

like image 967
Marcel Avatar asked Aug 24 '16 14:08

Marcel


People also ask

What is the purpose of a feature toggle?

In software development, a feature toggle is a mechanism that allows code to be turned “on” or “off” remotely without the need for a deploy. Feature toggles are commonly used by product, engineering, and DevOps teams for canary releases, A/B testing, and continuous deployment.

Are feature toggles good?

Feature toggles facilitate continuous delivery ' - even when they are agile developers or UX people. Feature toggles can help decouple delivery - moving changes to production - from release - enabling it for users - for example because what is meaningful for a user or customer comprises a bigger set of features.


1 Answers

You could use @ConditionalOnProperty annotation:

@Component
@ConditionalOnProperty(prefix = "myApplication.features", name = "feature1")
public class Feature1{
   // ...
}
like image 197
Maciej Marczuk Avatar answered Sep 25 '22 12:09

Maciej Marczuk