Togglz:java.lang.IllegalStateException:找不到FeatureManager

时间:2017-01-31 23:01:57

标签: java spring spring-mvc togglz

在将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的实现。” 添加下面的context-params后,这个错误消失了,但现在我有“找不到FeatureManager”

public class WebAppInitializer implements WebApplicationInitializer  {

    @Override
    public void onStartup(ServletContext servletContext) {

        servletContext.setInitParameter("org.togglz.FEATURE_MANAGER_PROVIDED", "true");
    }


 }

2 个答案:

答案 0 :(得分:0)

看起来Spring集成因某些原因而无法在您的应用中运行。基本上Togglz使用特定于Spring的BeanFinder实现来从Spring应用程序上下文中查找实例。这样,Togglz会查找TogglzConfig以引导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

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

我希望这会有所帮助。

答案 1 :(得分:0)

我对您有完全相同的问题,我发现我的情况的原因是TogglzFilter在Spring上下文中注册的TogglzConfig组件之前启动。因此,您必须确保在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" />

希望有帮助