Spring 个组件的功能切换

Feature-Toggle for Spring components

我有一个 Spring 启动应用程序,其中包含很多 @Component、@Controller、@RestController 注释组件。我想分别切换大约 20 种不同的功能。重要的是可以在不重建项目的情况下切换功能(重启就可以)。我认为 Spring 配置是个不错的方法。

我可以像这样想象一个配置 (yml):

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

主要问题是我不想在所有地方都使用 if 块。我宁愿完全禁用组件。例如,一个 @RestController 甚至应该被加载并且它不应该注册它的路径。我目前正在搜索类似这样的内容:

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

有这样的功能吗?有没有一种简单的方法可以自己实现?或者是否有其他功能切换的最佳实践?

顺便说一句:Spring 引导版本:1.3.4

试着看看ConditionalOnExpression

也许这应该有效

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

您可以使用 @ConditionalOnProperty 注释:

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

有条件地启用 bean - 禁用时为 null

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

@Autowired(required=false)
private Feature1 feature1;

如果条件 bean 是控制器,则不需要自动装配它,因为通常不会注入控制器。如果条件 bean 被注入,当它未启用时你会得到一个 No qualifying bean of type [xxx.Feature1],这就是你需要使用 required=false 自动装配它的原因。然后它将保持 null.

条件启用和禁用 bean

如果 Feature1 bean 被注入到其他组件中,您可以使用 required=false 注入它,或者在禁用该功能时将 bean 定义为 return:

@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;

条件启用和禁用 bean - Spring 配置:

@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个人资料

另一种选择是通过 spring 个配置文件激活 bean:@Profile("feature1")。但是,所有启用的功能都必须在单个 属性 spring.profiles.active=feature1, feature2... 中列出,因此我相信这不是您想要的。

FF4J is a framework to implement Feature Toggle pattern. It proposes a spring-boot starter and allows to enable or disable spring components at runtime through dedicated web console. By using AOP 它允许根据功能状态动态注入正确的 bean。它不会在 spring 上下文中添加或删除 bean。