A Guide to Flips for Spring – 春天的翻转指南

最后修改: 2018年 3月 23日


1. Overview


In this tutorial, we’ll have a look at Flips, a library that implements feature flags in the form of powerful annotations for Spring Core, Spring MVC, and Spring Boot applications.

在本教程中,我们将看看Flips,这个库以强大的注释形式为Spring Core、Spring MVC和Spring Boot应用程序实现功能标志。

Feature flags (or toggles) are a pattern for delivering new features quickly and safely. These toggles allow us to modify application behavior without changing or deploying new code. Martin Fowler’s blog has a very informative article about feature flags here.

功能标志(或称切换器)是一种快速、安全地提供新功能的模式。这些切换器使我们能够在不改变或部署新代码的情况下修改应用程序的行为Martin Fowler 的博客中有一篇关于功能标志的信息量很大的文章这里

2. Maven Dependency


Before we get started, we need to add the Flips library to our pom.xml:



Maven Central has the latest version of the library, and the Github project is here.

Maven Central有最新版本的库,而Github项目在这里

Of course, we also need to include a Spring:



Since Flips isn’t yet compatible with Spring version 5.x, we’re going to use the latest version of Spring Boot in the 4.x branch.

由于Flips还不兼容Spring 5.x版本,我们将使用4.x分支中的最新版本的Spring Boot

3. A Simple REST Service for Flips


Let’s put together a simple Spring Boot project for adding and toggling new features and flags.

让我们把一个简单的Spring Boot项目放在一起,用于添加和切换新功能和标志。

Our REST application will provide access to Foo resources:


public class Foo {
    private String name;
    private int id;

We’ll simply create a Service that maintains a list of Foos:


public class FlipService {

    private List<Foo> foos;

    public List<Foo> getAllFoos() {
        return foos;

    public Foo getNewFoo() {
        return new Foo("New Foo!", 99);

We’ll refer to additional service methods as we go, but this snippet should be enough to illustrate what FlipService does in the system.


And of course, we need to create a Controller:


public class FlipController {

    private FlipService flipService;

    // constructors

    public List<Foo> getAllFoos() {
        return flipService.getAllFoos();

4. Control Features Based on Configuration


The most basic use of Flips is to enable or disable a feature based on configuration. Flips has several annotations for this.


4.1. Environment Property


Let’s imagine we added a new capability to FlipService; retrieving Foos by their id.


Let’s add the new request to the controller:


  property = "feature.foo.by.id", 
  expectedValue = "Y")
public Foo getFooById(@PathVariable int id) {
    return flipService.getFooById(id)
      .orElse(new Foo("Not Found", -1));

The @FlipOnEnvironmentProperty controls whether or not this API is available.


Simply put, when feature.foo.by.id is Y, we can make requests by Id. If it isn’t (or not defined at all) Flips will disable the API method.


If a feature isn’t enabled, Flips will throw FeatureNotEnabledException and Spring will return “Not Implemented” to the REST client.

如果一个功能没有启用,Flips将抛出FeatureNotEnabledException,Spring将向REST客户端返回 “未实施”。

When we call the API with the property set to N, this is what we see:


Status = 501
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {
    "errorMessage": "Feature not enabled, identified by method 
      public com.baeldung.flips.model.Foo

As expected, Spring catches the FeatureNotEnabledException and returns status 501 to the client.


4.2. Active Profile


Spring has long given us the ability to map beans to different profiles, such as dev, test, or prod. Expanding on this capability to mapping feature flags to the active profile makes intuitive sense.

长期以来,Spring 为我们提供了将 Bean 映射到不同配置文件的能力,如devtestprod。将这一功能扩展到将功能标志映射到活动配置文件中,具有直观的意义。

Let’s see how features are enabled or disabled based on the active Spring Profile:

让我们看看如何根据活动的Spring Profile启用或禁用功能。

@RequestMapping(value = "/foos", method = RequestMethod.GET)
@FlipOnProfiles(activeProfiles = "dev")
public List getAllFoos() {
    return flipService.getAllFoos();

The @FlipOnProfiles annotation accepts a list of profile names. If the active profile is in the list, the API is accessible.


4.3. Spring Expressions


Spring’s Expression Language (SpEL) is the powerful mechanism for manipulating the runtime environment. Flips has us a way to toggle features with it as well.


@FlipOnSpringExpression toggles a method based on a SpEL expression that returns a boolean.


Let’s use a simple expression to control a new feature:


@FlipOnSpringExpression(expression = "(2 + 2) == 4")
public Foo getNewFoo() {
    return flipService.getNewFoo();

4.4. Disable


To disable a feature completely, use @FlipOff:


public Foo getFirstFoo() {
    return flipService.getLastFoo();

In this example, getFirstFoo() is completely inaccessible.


As we’ll see below, we can combine Flips annotations, making it possible to use @FlipOff to disable a feature based on the environment or other criteria.

正如我们将在下面看到的,我们可以结合Flips注释,使得使用@FlipOff 来根据环境或其他标准禁用一个功能成为可能。

5. Control Features With Date/Time


Flips can toggle a feature based on a date/time or the day of the week. Tying the availability of a new feature to the day or date has obvious advantages.


5.1. Date and Time


@FlipOnDateTime accepts the name of a property that is formatted in ISO 8601 format.

@FlipOnDateTime接受以ISO 8601格式表示的属性名称。

So let’s set a property indicating a new feature that will be active on March 1st:



Then we’ll write an API for retrieving the first Foo:


@FlipOnDateTime(cutoffDateTimeProperty = "first.active.after")
public Foo getFirstFoo() {
    return flipService.getLastFoo();

Flips will check the named property. If the property exists and the specified date/time have passed, the feature is enabled.


5.2. Day of Week


The library provides @FlipOnDaysOfWeek, which is useful for operations such as A/B testing:


@FlipOnDaysOfWeek(daysOfWeek={DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY})
public Foo getFooByNewId(@PathVariable int id) {
    return flipService.getFooById(id).orElse(new Foo("Not Found", -1));

getFooByNewId() is only available on Mondays and Wednesdays.


6. Replace a Bean


Switching methods on and off is useful, but we may want to introduce new behavior via new objects. @FlipBean directs Flips to call a method in a new bean.


A Flips annotation can work on any Spring @Component. So far, we’ve only modified our @RestController, let’s try modifying our Service.


We’ll create a new service with different behavior from FlipService:


public class NewFlipService {
    public Foo getNewFoo() {
        return new Foo("Shiny New Foo!", 100);

We will replace the old service’s getNewFoo() with the new version:


@FlipBean(with = NewFlipService.class)
public Foo getNewFoo() {
    return new Foo("New Foo!", 99);

Flips will direct calls to getNewThing() to NewFlipService. @FlipBean is another toggle that is most useful when combined with others. Let’s look at that now.


7. Combining Toggles


We combine toggles by specifying more than one. Flips evaluates these in sequence, with implicit “AND” logic. Therefore all of them must be true to toggle the feature on.

我们通过指定一个以上的切换器来组合切换器。Flips用隐含的 “AND “逻辑依次评估它们。因此,所有这些都必须是真实的,才能开启该功能。

Let’s combine two of our previous examples:


  with = NewFlipService.class)
  property = "feature.foo.by.id", 
  expectedValue = "Y")
public Foo getNewFoo() {
    return new Foo("New Foo!", 99);

We’ve made use of the new service configurable.


8. Conclusion


In this brief guide, we created a simple Spring Boot service and toggled APIs on and off using Flips annotations. We saw how features are toggled using configuration information and date/time, and also how features can be toggled by swapping beans at runtime.

在这个简短的指南中,我们创建了一个简单的Spring Boot服务,并使用Flips注解来切换API的开与关。我们看到了如何使用配置信息和日期/时间来切换功能,以及如何通过在运行时交换Bean来切换功能。

Code samples, as always, can be found over on GitHub.