0

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

切换控制器.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: Insted 我使用了 spring ConfigFile 并为单个属性创建了 bean,如下所示。
配置类.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:
4

1 回答 1

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);
            }
        };
    }
}
于 2019-11-01T13:39:03.963 回答