Spring 无法在控制器中使用FeatureManager自动连接配置文件

Spring 无法在控制器中使用FeatureManager自动连接配置文件,spring,spring-boot,java-8,autowired,togglz,Spring,Spring Boot,Java 8,Autowired,Togglz,我正试图用我的spring boot应用程序实现togglz。这里我的方法有两种。下面是两种用例方法的通用文件。ToggleController.java package com.learn.poc.toggle; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.we

我正试图用我的spring boot应用程序实现togglz。这里我的方法有两种。
下面是两种用例方法的通用文件。

ToggleController.java

package com.learn.poc.toggle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.togglz.core.manager.FeatureManager;

@Controller
public class ToggleController {
@Autowired
private FeatureManager manager;


@GetMapping("/testing")
@ResponseBody
String display() {

    if(manager.isActive(MyFeatures.FEATURE_ONE)){
        return "FeatureOne is active woking "+MyFeatures.FEATURE_ONE.isActive();
    }
    else {
        return "FeatureOne is Inactive redirected "+MyFeatures.FEATURE_ONE.isActive();
    }
  }
}
MyFeatures.java

package com.learn.poc.toggle;

import org.togglz.core.Feature;
import org.togglz.core.annotation.Label;
import org.togglz.core.context.FeatureContext;

public enum MyFeatures implements Feature {

@Label("First Feature")
FEATURE_ONE,

@Label("Second Feature")
FEATURE_TWO;

public boolean isActive() {
    return FeatureContext.getFeatureManager().isActive(this);
}
}
属性:(PS:我不想直接在应用程序属性中设置任何内容,如功能枚举等)

现在来看用例1:
我正在使用配置文件实现TogglzConfig。
mytogglzconfig.java

package com.learn.poc.toggle;
import java.io.File;
import org.springframework.stereotype.Component;
import org.togglz.core.Feature;
import org.togglz.core.manager.TogglzConfig;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.file.FileBasedStateRepository;
import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.core.user.UserProvider;

@Component
public class MyTogglzConfiguration implements TogglzConfig {
public Class<? extends Feature> getFeatureClass() {
    return MyFeatures.class;
}
public StateRepository getStateRepository() {
    return new FileBasedStateRepository(new File("PATH\features.properties"));
}
@Override
public UserProvider getUserProvider() {
    return new UserProvider() {
        @Override
        public FeatureUser getCurrentUser() {
            return new SimpleFeatureUser("admin", true);
        }
    };
}
}
如果按上述文件执行,则一切正常。我不知道第一个用例失败了。因为它在togglz文档中提到,如果您将配置文件提到@Component,它将自动连接到spring引导。
参考:


它说:

Togglz offers a special integration module for Spring. This module will automatically search for 
implementations of the TogglzConfig in Spring's ApplicationContext. Therefore you just have to 
declare your implementation as a class managed by Spring. If you are using the Spring annotation 
support you can just add a @Component annotation to your class:

您仍然需要在配置类中实现TogglzConfig。下面的类在Spring boot应用程序中为我工作

package com.cfa.restsolutions.menu.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.togglz.core.Feature;
import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.manager.TogglzConfig;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.jdbc.JDBCStateRepository;
import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.core.user.UserProvider;

import javax.sql.DataSource;

@Configuration
public class MyTogglzConfig implements TogglzConfig {

    @Autowired
    private DataSource dataSource;

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

    @Bean
    public FeatureProvider featureProvider() {
        return new EnumBasedFeatureProvider(MyFeatures.class);
    }

    @Bean
    @Override
    public StateRepository getStateRepository() {
        return new JDBCStateRepository(dataSource);
    }

    @Bean
    @Override
    public UserProvider getUserProvider() {
        return new UserProvider() {
            @Override
            public FeatureUser getCurrentUser() {
                return new SimpleFeatureUser("admin", true);
            }
        };
    }
}
package com.cfa.restsolutions.menu.configuration;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.togglz.core.Feature;
导入org.togglz.core.manager.EnumBasedFeatureProvider;
导入org.togglz.core.manager.TogglzConfig;
导入org.togglz.core.repository.StateRepository;
导入org.togglz.core.repository.jdbc.JDBCStateRepository;
导入org.togglz.core.spi.FeatureProvider;
导入org.togglz.core.user.FeatureUser;
导入org.togglz.core.user.SimpleFeatureUser;
导入org.togglz.core.user.UserProvider;
导入javax.sql.DataSource;
@配置
公共类MyTogglzConfig实现了TogglzConfig{
@自动连线
私有数据源;
@豆子
公共课
Togglz offers a special integration module for Spring. This module will automatically search for 
implementations of the TogglzConfig in Spring's ApplicationContext. Therefore you just have to 
declare your implementation as a class managed by Spring. If you are using the Spring annotation 
support you can just add a @Component annotation to your class:
package com.cfa.restsolutions.menu.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.togglz.core.Feature;
import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.manager.TogglzConfig;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.jdbc.JDBCStateRepository;
import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.core.user.UserProvider;

import javax.sql.DataSource;

@Configuration
public class MyTogglzConfig implements TogglzConfig {

    @Autowired
    private DataSource dataSource;

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

    @Bean
    public FeatureProvider featureProvider() {
        return new EnumBasedFeatureProvider(MyFeatures.class);
    }

    @Bean
    @Override
    public StateRepository getStateRepository() {
        return new JDBCStateRepository(dataSource);
    }

    @Bean
    @Override
    public UserProvider getUserProvider() {
        return new UserProvider() {
            @Override
            public FeatureUser getCurrentUser() {
                return new SimpleFeatureUser("admin", true);
            }
        };
    }
}