4

将 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");
    }


 }
4

2 回答 2

0

我和你有完全相同的问题,我发现我的情况的原因是 TogglzFilter 是在 TogglzConfig 组件在 spring 上下文中注册之前启动的。所以你必须确保在 web 容器启动的一开始就执行 spring 上下文注册。

所以我只是对我的 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" />

希望能帮助到你

于 2018-10-12T06:06:52.443 回答
-1

由于某种原因,Spring 集成似乎无法在您的应用程序中运行。基本上,Togglz 使用 Spring 特定的BeanFinder实现从 Spring 应用程序上下文中查找实例。通过这种方式,Togglz 查找TogglzConfig引导 a FeatureManager.

我的猜测是这里的这个块无法找到ApplicationContext你的应用程序。您可以通过设置相应的断点来验证这一点:

https://github.com/togglz/togglz/blob/2.3.0.Final/spring-web/src/main/java/org/togglz/spring/web/spi/SpringWebBeanFinder.java#L30-L40

还请确保 SpringContextLoaderListener已注册并正确调用。没有它,Togglz 查找代码将无法正常工作。

我希望这有帮助。

于 2017-02-01T05:44:11.853 回答