我想根据数据库值启用我的新功能。如果为该功能设置了数据库值,则应启用新代码。否则,它应该切换并流向旧代码。有没有办法在Java/Spring中实现这一目标?我不想经常打到数据库。我正在考虑在请求开始时一个呼叫。有什么例子吗?如果是这样,请告诉我。谢谢
创建功能类实现:
public enum CustomFeatures implements Feature {
@Label("Activates customFeature")
MY_CUSTOM_FEATURE;
public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
}
}
创建功能提供商:
@Bean
public FeatureProvider featureProvider() {
return new EnumBasedFeatureProvider(CustomFeatures.class);
}
创建实体和存储库:
@Entity
public class Feature {
private String name;
private Boolean active;
// getters-setters
}
创建@component,它将查询数据库并设置新功能SATE
@Component
public class FeatureToggler {
private final FeatureRepository featureRepository;
private final FeatureManager featureManager;
private FeatureToggler(FeatureRepository featureRepository, FeatureManager featureManager) {
this.featureRepository = featureRepository;
this.featureManager = featureManager;
}
@Schedule(fixedRate=60000)
public void refreshFeatureToggles() {
featureManager.setFeatureState(new FeatureState(CustomFeatures.MY_CUSTOM_FEATURE, featureRepository.findByName().isActive);
}
}
现在您可以使用检查是否启用了这样的功能:
if(CustomFeatures.MY_CUSTOM_FEATURE.isActive()) {
}
或使用春季。