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

    }
}


上面的Configuration应该可以正常工作,但是我正在获取“ 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");
    }


 }

最佳答案

看起来由于某种原因,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查找代码将无法正常工作。

我希望这有帮助。

09-25 21:03