gpt4 book ai didi

java - Togglz 引发 IllegalStateException 错误

转载 作者:行者123 更新时间:2023-11-30 08:11:17 28 4
gpt4 key购买 nike

在 Maven 上的 Web 应用程序中安装 togglz 时,我不断收到 IllegalStateException: Could not find the FeatureManager。我完全按照说明进行操作。在我的 Maven 应用程序中,我的 pom.xml 文件中有以下设置:

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-core</artifactId>
<version>2.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>2.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-servlet</artifactId>
<version>2.1.0.Final</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

以及 web.xml 文件中的以下内容:

<context-param>
<param-name>org.togglz.core.manager.TogglzConfig</param-name>
<param-value>com.test.test.ana.FeatureFlagConfiguration</param-value>
</context-param>

<servlet>
<servlet-name>TogglzConsoleServlet</servlet-name>
<servlet-class>org.togglz.console.TogglzConsoleServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TogglzConsoleServlet</servlet-name>
<url-pattern>/togglz/*</url-pattern>
</servlet-mapping>

这个示例 Maven 应用程序运行良好,但当我尝试转到 togglz 虚拟目录时,它会抛出 IllegalStateException: Could not find the FeatureManager 错误,它没有提供比这更多的详细信息。关于该错误的真正含义有什么建议吗?我只是想获得一个使用有效的 togglz 的示例项目。我不能使用 spring 或 cdi,只能使用 servlet。 (是的,我配置了 servlet 3.0)

提前致谢,

编辑:这是确切的堆栈跟踪:

    [ERROR   ] SRVE0777E: Exception thrown by application class 'org.togglz.core.context.FeatureContext.getFeatureManager:49'
java.lang.IllegalStateException: Could not find the FeatureManager. For web applications please verify that the TogglzFilter starts up correctly. In other deployment scenarios you will typically have to implement a FeatureManagerProvider as described in the 'Advanced Configuration' chapter of the documentation.
at org.togglz.core.context.FeatureContext.getFeatureManager(FeatureContext.java:49)
at org.togglz.core.manager.LazyResolvingFeatureManager.getDelegate(LazyResolvingFeatureManager.java:24)
at org.togglz.core.manager.LazyResolvingFeatureManager.getCurrentFeatureUser(LazyResolvingFeatureManager.java:49)
at org.togglz.console.TogglzConsoleServlet.isFeatureAdmin(TogglzConsoleServlet.java:68)
at org.togglz.console.TogglzConsoleServlet.service(TogglzConsoleServlet.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1285)
at [internal classes]

最佳答案

如果您处于普通的 Servlet 环境中,添加 Maven 依赖项后需要执行以下操作:

实现您的功能枚举

这通常看起来像这样:

public enum MyFeatures implements Feature {

@EnabledByDefault
@Label("First Feature")
FEATURE_ONE,

@Label("Second Feature")
FEATURE_TWO;

public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
}

}

通过实现 TogglzConfig 来配置 Togglz

一个典型的例子如下:

public class MyTogglzConfiguration implements TogglzConfig {

public Class<? extends Feature> getFeatureClass() {
return MyFeatures.class;
}

public StateRepository getStateRepository() {
return new FileBasedStateRepository(new File("/tmp/features.properties"));
}

public UserProvider getUserProvider() {
return new ServletUserProvider();
}

}

在 web.xml 中注册您的配置类

在普通的 Servlet 环境中,您现在必须通过向 web.xml 添加类似内容来注册 TogglzConfig 实现:

<context-param>
<param-name>org.togglz.core.manager.TogglzConfig</param-name>
<param-value>com.example.myapp.MyTogglzConfiguration</param-value>
</context-param>

您还应该明确告诉 Togglz,您​​不希望它从 Spring 或 CDI 查找 FeatureManager,而是创建和管理实例本身:

<context-param>
<param-name>org.togglz.FEATURE_MANAGER_PROVIDED</param-name>
<param-value>true</param-value>
</context-param>

在 Servlet 3.0 环境中,通常会自动选取 TogglzFilter。不过,您也可以手动注册:

<filter>
<filter-name>TogglzFilter</filter-name>
<filter-class>org.togglz.servlet.TogglzFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TogglzFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

我希望这有帮助。如果这仍然不起作用,请在您的问题中包含完整的堆栈跟踪。

关于java - Togglz 引发 IllegalStateException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30360224/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com