微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将“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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?