Spring中@RefreshScope注解的處理方法詳解
@RefreshScope注解
spring啟動(dòng)時(shí)會(huì)調(diào)用ClassPathBeanDefinitionScanner.java類中的doScan()對(duì)包路徑下的所有class進(jìn)行掃描,獲取bean的定義,同時(shí)對(duì)bean的@RefreshScope(@Scope的父類)進(jìn)行處理。
- 獲取作用在類上的@RefreshScope(@Scope的父類)注解元數(shù)據(jù),封裝成ScopeMetadata對(duì)象,同時(shí)聲明bean的作用域candidate.setScope(scopeMetadata.getScopeName());
- 根據(jù)scopeMetadata即@Scope對(duì)bean進(jìn)行代理,使用ScopedProxyFactoryBean類創(chuàng)建RootBeanDefinition代替原理的bean定義。將被代理的原始bean定義注冊(cè)到容器中(scopedTarget.configController -> ConfigController),再將代理的bean定義注冊(cè)到容器中(configController -> ScopedProxyFactoryBean)
處理這個(gè)使用@RefreshScope注解的bean的是RefreshScope組件,可以看到父類GenericScope實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor接口,這是容器的后置處理器,當(dāng)容器啟動(dòng)時(shí)會(huì)先調(diào)用BeanDefinitionRegistryPostProcessor接口中的postProcessBeanDefinitionRegistry()方法,再BeanFactoryPostProcessor接口中的postProcessBeanFactory()方法。
public class RefreshScope extends GenericScope implements ApplicationContextAware, Ordered{...} public class GenericScope implements Scope, BeanFactoryPostProcessor, BeanDefinitionRegistryPostProcessor, DisposableBean{...}
獲取容器中包含的所有的bean定義,再通過bean裝飾的bean定義獲取作用域,判斷是否為RefreshScope能處理的作用域,在構(gòu)造RefreshScope實(shí)例時(shí)設(shè)置了name為"refresh"getName().equals(root.getDecoratedDefinition().getBeanDefinition().getScope())。ps:這是個(gè)細(xì)節(jié),在被裝飾的bean定義的scope是"refresh",而裝飾者bean的scope是"";之后處理的時(shí)候先處理裝飾者bean,在處理原始bean,就是通過這個(gè)scope來判斷的。如果是的話將bean的class設(shè)置為LockedScopedProxyFactoryBean,將當(dāng)前RefreshScope實(shí)例對(duì)象設(shè)置為構(gòu)造函數(shù)的參數(shù)。看下LockedScopedProxyFactoryBean這個(gè)類:
public static class LockedScopedProxyFactoryBean<S extends GenericScope> extends ScopedProxyFactoryBean implements MethodInterceptor{...} public class ScopedProxyFactoryBean extends ProxyConfig implements FactoryBean<Object>, BeanFactoryAware{...}
可以看到LockedScopedProxyFactoryBean的父類實(shí)現(xiàn)了BeanFactoryAware接口,因此在bean實(shí)例化的時(shí)候會(huì)調(diào)用實(shí)現(xiàn)的setBeanFactory()方法,這時(shí)會(huì)通過代理工廠創(chuàng)建代理對(duì)象,即生成LockedScopedProxyFactoryBean類的代理對(duì)象。由于LockedScopedProxyFactoryBean類實(shí)現(xiàn)了Advised接口,因此可以作為代理對(duì)象的攔截器,當(dāng)調(diào)用代理對(duì)象的方法時(shí)攔截器就會(huì)起作用。
public void setBeanFactory(BeanFactory beanFactory) { ...... ProxyFactory pf = new ProxyFactory(); pf.copyFrom(this); pf.setTargetSource(this.scopedTargetSource); ..... this.proxy = pf.getProxy(cbf.getBeanClassLoader()); } public void setBeanFactory(BeanFactory beanFactory) { super.setBeanFactory(beanFactory); Object proxy = getObject(); if (proxy instanceof Advised) { Advised advised = (Advised) proxy; advised.addAdvice(0, this); } }
總結(jié)下@RefreshScope注解的bean實(shí)例化的過程:
- 使用了@RefreshScope注解的bean的定義會(huì)被設(shè)置成LockedScopedProxyFactoryBean.class
- 創(chuàng)建bean時(shí)先生成LockedScopedProxyFactoryBean的實(shí)例對(duì)象
- 由于LockedScopedProxyFactoryBean實(shí)現(xiàn)了BeanFactoryAware接口,會(huì)調(diào)用setBeanFactory()
- 在setBeanFactory()方法中會(huì)通過cglib創(chuàng)建LockedScopedProxyFactoryBean對(duì)象的代理對(duì)象,F(xiàn)actoryBean接口的getObject()會(huì)返回此代理對(duì)象,因此最終得到的就是LockedScopedProxyFactoryBean對(duì)象的代理對(duì)
- 由于代理對(duì)象為LockedScopedProxyFactoryBean的子類,因此實(shí)現(xiàn)了Advised接口,可以將調(diào)用setBeanFactory()方法的LockedScopedProxyFactoryBean對(duì)象作為攔截器保存下來
當(dāng)容器啟動(dòng)完成刷新后會(huì)發(fā)布ContextRefreshedEvent事件,RefreshScope類中的start()使用了@EventListener注解,可以作為監(jiān)聽器對(duì)ContextRefreshedEvent事進(jìn)行處理。找到scope是"refresh"的bean的定義,再走Scope的代碼塊進(jìn)行實(shí)例化。
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { ...... if (mbd.isSingleton()) { ...... } else if (mbd.isPrototype()) { ...... } else { String scopeName = mbd.getScope(); //根據(jù)name獲取Scope,當(dāng)前一共有四個(gè) //refresh -> {RefreshScope@5469} //request -> {RequestScope@7462} //session -> {SessionScope@7464} //application -> {ServletContextScope@7466} final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, () -> { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } } ...... }
獲取到RefreshScope對(duì)象后就會(huì)調(diào)用其中的get()方法進(jìn)行初始化。先創(chuàng)建BeanLifecycleWrapper對(duì)象,包含了bean的name和創(chuàng)建bean的工廠方法(lamdba表達(dá)式),再調(diào)用BeanLifecycleWrapper類中的getBean()方法獲取bean的實(shí)例對(duì)象,此時(shí)調(diào)用this.bean = this.objectFactory.getObject();工廠方法來創(chuàng)建bean,則回到createBean(beanName, mbd, args);方法完成bean的創(chuàng)建。接下來看看方法是怎么調(diào)用的。
- 獲取被代理原始bean的實(shí)例對(duì)象
- 創(chuàng)建攔截器鏈,找到之前加入的LockedScopedProxyFactoryBean攔截器,調(diào)用invoke()方法進(jìn)行處理
- 獲取代理對(duì)象,即LockedScopedProxyFactoryBean的代理對(duì)象;獲取讀寫鎖的讀鎖,當(dāng)前配置刷新時(shí),則無法獲取
- 以反射方式調(diào)用目標(biāo)方法method.invoke(target, args)
再看看配置怎么刷新到bean當(dāng)中的。當(dāng)配置源變更時(shí)方法調(diào)用鏈如下:
- NacosContextRefresher#registerNacosListener()注冊(cè)監(jiān)聽器(receiveConfigInfo())
- ApplicationEventPublisher.java#publishEvent()發(fā)布RefreshEvent事件
- SimpleApplicationEventMulticaster#multicastEvent()方法廣播事件
- RefreshEventListener#handle(RefreshEvent event)處理RefreshEvent事件
- ContextRefresher#refresh()進(jìn)行處理,做了三件事
- 刷新配置源
- 發(fā)布EnvironmentChangeEvent事件,重新生成配置源Bean(@ConfigurationProperties注解Bean)
- 將配置寫到@RefreshScope注解標(biāo)注的Bean
開始分析怎么刷新到@RefreshScope注解標(biāo)注的Bean:將保存原始被代理的Bean的BeanLifecycleWrapperCache中清空,獲取寫鎖(此時(shí)其他線程無法獲取讀鎖),調(diào)用每個(gè)BeanLifecycleWrapper的destroy()方法進(jìn)行銷毀。緩存被清空了,如果重新獲取Bean就得重新創(chuàng)建,此時(shí)就會(huì)進(jìn)行Bean的屬性填充,就可以從屬性源中獲取新的屬性了
到此這篇關(guān)于Spring中@RefreshScope注解的處理方法詳解的文章就介紹到這了,更多相關(guān)Spring的@RefreshScope注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot Admin監(jiān)控服務(wù)如何使用
這篇文章主要介紹了Spring Boot Admin監(jiān)控服務(wù)如何使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)
下面小編就為大家?guī)硪黄狫AVA 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08SpringBoot實(shí)現(xiàn)文件上傳與下載功能的示例代碼
文件上傳與下載是Web應(yīng)用開發(fā)中常用的功能之一。接下來我們將討論如何在Spring?Boot的Web應(yīng)用開發(fā)中,如何實(shí)現(xiàn)文件的上傳與下載,感興趣的可以了解一下2022-06-06Java最簡單的DES加密算法實(shí)現(xiàn)案例
下面小編就為大家?guī)硪黄狫ava最簡單的DES加密算法實(shí)現(xiàn)案例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06spring boot實(shí)現(xiàn)阿里云視頻點(diǎn)播上傳視頻功能(復(fù)制粘貼即可)
這篇文章主要介紹了spring boot實(shí)現(xiàn)阿里云視頻點(diǎn)播上傳視頻功能(復(fù)制粘貼即可),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12springboot中在非bean類中調(diào)用bean的實(shí)現(xiàn)方法
在Spring Boot中,非Bean類調(diào)用Bean方法通常需要通過靜態(tài)方法獲取Bean實(shí)例,然后調(diào)用相應(yīng)的方法,這種方法避免了直接在非Bean類中注入Bean,保持了代碼的簡潔和可維護(hù)性,通過這種方式,可以在不改變?cè)写a結(jié)構(gòu)的情況下,實(shí)現(xiàn)Bean方法的調(diào)用2025-02-02Java中的javaBean、vo、entity、domain和pojo
這篇文章主要介紹了Java中的javaBean、vo、entity、domain和pojo用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12