1. 使用@value 注解,配置实时更新,使用@ConfigurationProperties 注解配置无实时更新
2.添加监听配置,配置还没有实时更新
@Configuration
public class ConfigRefresh {
@Autowired
private ApplicationContext cxt;
@ApolloConfigChangeListener
public void onChange(ConfigChangeEvent event) {
Map<String, Object> configBeanMap = cxt.getBeansWithAnnotation(ConfigurationProperties.class);
if(null != configBeanMap && !configBeanMap.isEmpty()) {
Set<String> keys = new HashSet<>();
for(Map.Entry<String, Object> entry : configBeanMap.entrySet()) {
if(null != entry) {
String className = entry.getKey();
Object configClass = entry.getValue();
if(configClass != null && null != className && !"".equals(className)) {
ConfigurationProperties configurationProperties = configClass.getClass().getAnnotation(ConfigurationProperties.class);
if(configurationProperties != null) {
String prefix = configurationProperties.prefix();
if(null != prefix && !"".equals(prefix.trim())) {
for(String changeKey : event.changedKeys()) {
if(changeKey.startsWith(prefix)) {
keys.add(changeKey);
}
}
}
}