網站首頁 編程語言 正文
上一篇講配置變更時會發布RefreshEvent事件,監聽這個事件可以實現配置刷新。具體看一下。
1、監聽RefreshEvent
//org.springframework.cloud.endpoint.event.RefreshEventListener#onApplicationEvent
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationReadyEvent) {
handle((ApplicationReadyEvent) event);
}
else if (event instanceof RefreshEvent) {
handle((RefreshEvent) event);
}
}
//org.springframework.cloud.endpoint.event.RefreshEventListener#handle
public void handle(RefreshEvent event) {
if (this.ready.get()) { // don't handle events before app is ready
log.debug("Event received " + event.getEventDesc());
//調用refresh
Set<String> keys = this.refresh.refresh();
log.info("Refresh keys changed: " + keys);
}
}
//org.springframework.cloud.context.refresh.ContextRefresher#refresh
public synchronized Set<String> refresh() {
Set<String> keys = refreshEnvironment();
this.scope.refreshAll();
return keys;
}
1.1、發布EnvironmentChangeEvent
//org.springframework.cloud.context.refresh.ContextRefresher#refreshEnvironment
public synchronized Set<String> refreshEnvironment() {
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(this.context, keys));
return keys;
}
1.2、銷毀beans
//org.springframework.cloud.context.scope.refresh.RefreshScope#refreshAll
public void refreshAll() {
//銷毀bean
super.destroy();
//發布事件,沒什么實現
this.context.publishEvent(new RefreshScopeRefreshedEvent());
}
循環銷毀bean
//org.springframework.cloud.context.scope.GenericScope#destroy()
public void destroy() {
List<Throwable> errors = new ArrayList<Throwable>();
Collection<BeanLifecycleWrapper> wrappers = this.cache.clear();
for (BeanLifecycleWrapper wrapper : wrappers) {
try {
Lock lock = this.locks.get(wrapper.getName()).writeLock();
lock.lock();
try {
wrapper.destroy();
}
finally {
lock.unlock();
}
}
catch (RuntimeException e) {
errors.add(e);
}
}
if (!errors.isEmpty()) {
throw wrapIfNecessary(errors.get(0));
}
this.errors.clear();
}
2、監聽EnvironmentChangeEvent
//org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder#onApplicationEvent
public void onApplicationEvent(EnvironmentChangeEvent event) {
if (this.applicationContext.equals(event.getSource())
// Backwards compatible
|| event.getKeys().equals(event.getSource())) {
rebind();
}
}
重新綁定
//org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder#rebind()
public void rebind() {
this.errors.clear();
for (String name : this.beans.getBeanNames()) {
rebind(name);
}
}
循環bean重新綁定
public boolean rebind(String name) {
if (!this.beans.getBeanNames().contains(name)) {
return false;
}
if (this.applicationContext != null) {
try {
//獲取bean
Object bean = this.applicationContext.getBean(name);
//如果是代理類,獲取被代理對象
if (AopUtils.isAopProxy(bean)) {
bean = ProxyUtils.getTargetObject(bean);
}
if (bean != null) {
// TODO: determine a more general approach to fix this.
// see https://github.com/spring-cloud/spring-cloud-commons/issues/571
if (getNeverRefreshable().contains(bean.getClass().getName())) {
return false; // ignore
}
//銷毀bean
this.applicationContext.getAutowireCapableBeanFactory()
.destroyBean(bean);
//初始化bean
this.applicationContext.getAutowireCapableBeanFactory()
.initializeBean(bean, name);
return true;
}
}
catch (RuntimeException e) {
this.errors.put(name, e);
throw e;
}
catch (Exception e) {
this.errors.put(name, e);
throw new IllegalStateException("Cannot rebind to " + name, e);
}
}
return false;
}
通過將原來的bean銷毀并且重新創建一個全新的bean來實現配置的刷新。這樣做有個問題,和原來的bean綁定的bean,可能就不能用了。還有一些bean重的緩存,比如說數據庫連接池,也被銷毀了。
原文鏈接:https://blog.csdn.net/xuwenjingrenca/article/details/125155614
相關推薦
- 2022-10-27 python圖像填充與裁剪/resize的實現代碼_python
- 2024-07-13 SpringBoot入門(解決JDK8不存在問題)
- 2023-01-19 Android?任務棧機制詳解_Android
- 2023-03-25 Python實現在Excel中繪制可視化大屏的方法詳解_python
- 2022-08-02 Android自定義Dialog的方法實例_Android
- 2023-12-14 【cchardet模塊】報出 “from cchardet import _cchardet Imp
- 2022-07-28 C++圖文并茂講解類型轉換函數_C 語言
- 2022-07-03 級聯分類器算法原理解析_相關技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支