将 Togglz 与我的 Spring MVC 应用程序集成时遇到异常。
异常
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.
org.togglz.core.context.FeatureContext.getFeatureManager(FeatureContext.java:53)
org.togglz.core.manager.LazyResolvingFeatureManager.getDelegate(LazyResolvingFeatureManager.java:24)
org.togglz.core.manager.LazyResolvingFeatureManager.getCurrentFeatureUser(LazyResolvingFeatureManager.java:49)
org.togglz.console.TogglzConsoleServlet.isFeatureAdmin(TogglzConsoleServlet.java:75)
org.togglz.console.TogglzConsoleServlet.service(TogglzConsoleServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.togglz.servlet.TogglzFilter.doFilter(TogglzFilter.java:100)
以下依赖项在 maven 中定义 -
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-core</artifactId>
<version>2.3.0.Final</version>
</dependency>
<!-- Spring integration (optional) -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-web</artifactId>
<version>2.3.0.Final</version>
</dependency>
<!-- Togglz Admin Console -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>2.3.0.Final</version>
</dependency>
我的配置类 -
@Component
public class MyTogglzConfiguration implements TogglzConfig {
/* ..... */
@Override
public UserProvider getUserProvider() {
return new UserProvider() {
@Override
public FeatureUser getCurrentUser() {
return new SimpleFeatureUser("admin", true);
}
};
}
@Override
public Class<? extends Feature> getFeatureClass() {
// TODO Auto-generated method stub
return MyFeatures.class;
}
@Override
public StateRepository getStateRepository() {
// TODO Auto-generated method stub
return new FileBasedStateRepository(new File("c:/tmp/features.properties"));
}
}
我的要素类-
public enum MyFeatures implements Feature {
@Label("First Feature")
FEATURE_ONE,
@EnabledByDefault
@Label("Second Feature")
FEATURE_TWO;
public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
}
}
上述配置应该有效,但我收到“java.lang.IllegalStateException:找不到 TogglzConfig 或 TogglzBootstrap 的任何实现”。添加以下上下文参数后,此错误消失了,但现在我“找不到 FeatureManager”
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) {
servletContext.setInitParameter("org.togglz.FEATURE_MANAGER_PROVIDED", "true");
}
}
我和你有完全相同的问题,我发现我的情况的原因是 TogglzFilter 在 Spring 上下文中注册 TogglzConfig 组件之前启动。因此,您必须确保 Spring 上下文注册在 Web 容器启动之初执行。
所以我只是对 web.xml 进行了以下更改来解决它。
更改前:
<display-name>REST HelloWorld</display-name>
<servlet>
<servlet-name>rest-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Welcome files -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
更改后:
<display-name>REST HelloWorld</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>rest-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Welcome files -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
请注意,用于上下文注册的组件扫描是在文件rest-dispatcher-servlet.xml中定义的
<context:component-scan
base-package="com.xxx.togglz" />
希望对你有帮助
我是一名优秀的程序员,十分优秀!