當(dāng)前位置:
首頁(yè) >
Spring5源码 - 14 如何在所有Bean创建完后做扩展?
發(fā)布時(shí)間:2025/3/21
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Spring5源码 - 14 如何在所有Bean创建完后做扩展?
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 葛大爺?shù)膯?wèn)題
- Answer
- 方式一 基于SmartInitializingSingleton接口
- Source
- Code
- 方式二 基于Spring事件監(jiān)聽(tīng)
- Source
- Code
- 驗(yàn)證
葛大爺?shù)膯?wèn)題
Answer
想要回答這個(gè)問(wèn)題,就要對(duì)Spring的生命周期有一定的了解,今天我們就來(lái)回顧一下IOC的生命周期及Spring提供給開(kāi)發(fā)人員的擴(kuò)展點(diǎn),當(dāng)然了,我們今天只聊Bean加載完成后的事兒 。
老規(guī)矩 先應(yīng)用后源碼 ,開(kāi)搞~
AAA BBB CCC 均是spring管理的bean
@Component public class AAA {public AAA() {System.out.println("AAA init");} }方式一 基于SmartInitializingSingleton接口
Source
生命周期中倒數(shù)第二步
// Instantiate all remaining (non-lazy-init) singletons.finishBeanFactoryInitialization(beanFactory);SmartInitializingSingleton接口是在所有的Bean實(shí)例化完成以后,Spring回調(diào)的方法, 所以這里也是一個(gè)擴(kuò)展點(diǎn),可以在單例bean全部完成實(shí)例化以后做處理。
Code
【配置類】
package com.artisan.beanLoadedExtend.smartinit;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@Configuration @ComponentScan("com.artisan.beanLoadedExtend") public class SmartInitConfig {}【擴(kuò)展類 implements SmartInitializingSingleton 】
package com.artisan.beanLoadedExtend.smartinit;import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.stereotype.Component;@Component public class SmartInitExtend implements SmartInitializingSingleton {@Overridepublic void afterSingletonsInstantiated() {System.out.println("all singleton beans loaded , 自定義擴(kuò)展here ");} }【測(cè)試】
package com.artisan.beanLoadedExtend.smartinit;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SmartInitConfig.class);} }方式二 基于Spring事件監(jiān)聽(tīng)
Source
生命周期的最后一步是finishRefresh();,這里面中有一個(gè)方法是publishEvent
所以這里也可以進(jìn)行擴(kuò)展,監(jiān)聽(tīng)ContextRefreshedEvent事件 。
Code
【配置類】
package com.artisan.beanLoadedExtend.listener;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.artisan.beanLoadedExtend") public class Config { }【基于接口的方式】
package com.artisan.beanLoadedExtend.listener;import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component;@Component public class BeanLoadedExtendListener implements ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {System.out.println("監(jiān)聽(tīng)到ContextRefreshedEvent, 自定義擴(kuò)展here ");} }【基于注解的方式】
package com.artisan.beanLoadedExtend.listener;import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component;@Component public class BeanLoadedExtendListenerByAnno {@EventListener(ContextRefreshedEvent.class)public void extend(){System.out.println("基于@EventListener的監(jiān)聽(tīng)");} }二選一,推薦基于注解的方式
【測(cè)試】
package com.artisan.beanLoadedExtend.listener;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);}}驗(yàn)證
總結(jié)
以上是生活随笔為你收集整理的Spring5源码 - 14 如何在所有Bean创建完后做扩展?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring5源码 - 13 Sprin
- 下一篇: 深入理解分布式技术 - Dubbo vs