将“Togglz”功能切换库添加到 Spring Boot REST API

问题描述

我正在尝试使用 Togglz 库,它允许您包装应用程序逻辑,并能够通过一些高级策略将其切换为 ON 或 OFF。我正在浏览 Spring Boot 文档,虽然它非常简洁,但我发现它缺少一些信息,这些信息不允许我正确测试。

参考: https://www.togglz.org/documentation/spring-boot-starter.html

  1. 我正在运行一个 Spring Boot 2.4.5 版本的项目,该文档说要导入依赖项,我这样做了:
<dependency>
  <groupId>org.togglz</groupId>
  <artifactId>togglz-spring-boot-starter</artifactId>
  <version>2.6.1.Final</version>
</dependency>
  1. 然后文档说明您可以在 @RestController 上使用自动配置类,例如
@Controller
public class MyClass {
  private FeatureManager manager;

  public MyClass(FeatureManager manager) {
      this.manager = manager;
  }

  @RequestMapping("/")
  public ResponseEntity<?> index() {
      if (manager.isActive(HELLO_WORLD)) {
           ...
      }
  }
}

这里已经有一些我没有看到解释的问题,首先,将枚举“HELLO_WORLD”作为参数传递给 FeatureManager 上的此 isActive() 函数。我不明白他们是如何将它注入到方法/类中的。他们确实展示了如何在 yaml 中声明功能 ENUM,但是,这不是引用传递到前面提到的 isActive() 方法中的“HELLO_WORLD”,即:

togglz:
  features:
    FOO:
      enabled: true
    BAR:
      enabled: false

进一步阅读文档,他们最终确实引用了这个 HELLO_WORLD 枚举,但我尝试将其添加到我的 application.yaml 中,但我似乎无法弄清楚他们如何将这些功能枚举注入到这些方法中:

togglz:
  enabled: true # Enable Togglz for the application.
  features: # The feature states. Only needed if feature states are stored in application properties.
    HELLO_WORLD:
      enabled: true

文档确实解释了如何为这些功能创建枚举类,但他们明确地将其列为在 yaml 文件中定义它的替代方法

public enum MyFeatures implements Feature {

    @EnabledByDefault
    @Label("First Feature")
    FEATURE_ONE,@Label("Second Feature")
    FEATURE_TWO;
}

@Bean
public FeatureProvider featureProvider() {
    return new EnumBasedFeatureProvider(MyFeatures.class);
}

我也试过这个,当我尝试运行应用程序时,我得到了更多的 Bean 异常错误,即

Description:

Parameter 2 of method featureManager in org.togglz.spring.boot.autoconfigure.TogglzAutoConfiguration$FeatureManagerConfiguration required a bean of type 'org.togglz.core.user.UserProvider' that could not be found.


Action:

Consider defining a bean of type 'org.togglz.core.user.UserProvider' in your configuration.

成功使用此库的任何人都可以提供输入如何设置简单的功能切换e ?最终,我希望能够在应用程序使用 RELEASE DATE activation 策略(即 2021-06-30 00:00:00)时打开/关闭此功能,以便我可以激活切换基于日期时间。

参考:https://www.togglz.org/documentation/activation-strategies.html

这可以在 yaml 中完成吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...