Spring Cloud @RefreshScope刷新問題


問題

使用@RefreshScope會刷新在sprign ioc中所有bean中使用@Value的值,但是在配置類中使用方法去配置的其他類參數並不會改變例如

解決方案

//使用此方法監聽事件
@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;
}

具體原因請參考
詳情


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM