springboot1.x apollo 更改属性值不起作用。 ConfigurationProperties

1. @ApolloConfigChangeListeners 默认监控命名空间是 application.properties , 如果是自己创建的namespace ,一定要明确指定(包含文件扩展名)。

 @ApolloConfigChangeListener(value = {"xx.yml"})
public void onChange(ConfigChangeEvent configChangeEvent) {
ConfigChange change;
System.out.println("Xxx");
}
同理如果使用手动方式注册监听器的时候也 应明确指定命名空间。(同样注意一定加上文件扩展名)
ConfigChangeListener configChangeListener = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent configChangeEvent) {
ConfigChange change;
System.out.println("Xxx");
}
};
ConfigService.getConfig("xx.yml").addChangeListener(configChangeListener);
注意这里的getConfig ,还有一个getAppConfig 还有一个 getConfigFile 。 其中前两个对应的监听执行器是AbstractConfig , 后一个是 AbstractConfigFile 。
这三个方法, 其中 getAppCofnig是指默认默认空间 application ,但是调试发现其参数是不带文件扩展名称的。
public static Config getAppConfig() {
return getConfig(ConfigConsts.NAMESPACE_APPLICATION);
}
String NAMESPACE_APPLICATION = "application";
为什么我使用getConfig,其入参也是namespace ,为什么我传入参数时 必须加上扩展名称呢???(不加的时候,数据发生变化时,根本就不调用你定义的监听器,搞了半天。)
public static Config getConfig(String namespace) {
return s_instance.getManager().getConfig(namespace);
}
还有一个getConfigFile 这个是监听整个配置文件的,文件变更即调用,但是请求值是整个文件,需要自己对比哪些值发生变化。

问题: getConfig()监控的yml 文件,我是对应的bean @ConfigurationProperties("defines") . 如何将这个变化同步到bean呢?



原文地址:https://www.cnblogs.com/zhangchenglzhao/p/15740243.html