无法使用控制器中的FeatureManager自动装配配置文件

时间:2019-09-20 11:10:14

标签: 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.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);
}
}

Application.properties:(PS:我不想在应用程序属性中直接设置任何功能,例如功能枚举等)

togglz.console.enabled=true
togglz.console.path=/togglz-console
server.servlet.context-path=/api
server.port=8080
togglz.console.use-management-port=false
#togglz.console.secured=false
#togglz.feature-enums=com.learn.poc.toggle.MyFeatures

现在可以使用UseCase-1:
我正在使用配置文件来实现TogglzConfig。
MyTogglzConfiguration.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);
        }
    };
}
}

因此,如果我按照上述方式操作,则不会自动接线,即,不会从路径中获取属性文件,也不会获得任何功能。

UseCase-2:插入后,我使用了Spring ConfigFile并为单个属性创建了bean,如下所示。
configClass.java

package com.learn.poc.toggle;

import java.io.File;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.file.FileBasedStateRepository;
import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.core.user.UserProvider;

@Configuration
public class configClass {

@Bean
public FeatureProvider featureProvider() {
    return new EnumBasedFeatureProvider(MyFeatures.class);
}
@Bean
public StateRepository getStateRepository() {
    System.out.println("in state");
    return new FileBasedStateRepository(new File("PATH\features.properties"));
}

@Bean
public UserProvider getUserProvider() {
    System.out.println("in user provider");
    return new UserProvider() {
        @Override
        public FeatureUser getCurrentUser() {
            return new SimpleFeatureUser("admin", true);
        }
    };
}
}

如果执行上述文件,则一切正常。我不知道第一个用例失败了。因为它在togglz文档中提到,如果您将配置文件称为@Component,它将自动接线以进行Spring Boot。
引用:https://www.togglz.org/documentation/configuration.html


它说:

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:

1 个答案:

答案 0 :(得分:0)

您仍然需要在配置类中实现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);
            }
        };
    }
}