使用@RefreshScope会刷新在sprign ioc中所有bean中使用@Value的值,但是在配置类中使用方法去配置的其他类参数并不会改变例如
Spring Cloud @RefreshScope刷新问题

解决方案

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

原因

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

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

具体原因请参考
详情

相关文章:

  • 2021-06-06
  • 2021-06-04
  • 2021-08-22
  • 2022-12-23
  • 2023-03-20
  • 2022-12-23
  • 2021-07-17
猜你喜欢
  • 2021-12-22
  • 2021-10-20
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
相关资源
相似解决方案