spring cloud @RefreshScope刷新问题

问题

使用@RefreshScope会刷新在sprign ioc中所有bean中使用@Value的值,但是在配置类中使用方法去配置的其他类参数并不会改变例如
spring cloud @RefreshScope刷新问题_第1张图片

解决方案

//使用此方法监听事件
@EventListener
    public void envListener(EnvironmentChangeEvent event) {
    }

原因

在调用刷新方法是会产生一个EnvironmentChangeEvent事件。
其实进入 ContextRefresher 的源码,看下refresh接口,就很明确了

public synchronized Set refresh() {
    Map before = extract(
            this.context.getEnvironment().getPropertySources());
    addConfigFilesToEnvironment();
    Set keys = changes(before,
            extract(this.context.getEnvironment().getPropertySources())).keySet();
    // 注意这一行,抛出了一个变更事件
    this.context.publishEvent(new EnvironmentChangeEvent(context, keys));
    this.scope.refreshAll();
    return keys;
}

具体原因请参考
详情

你可能感兴趣的:(spring cloud @RefreshScope刷新问题)