當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring5源码 - 14 如何在所有Bean创建完后做扩展?
生活随笔
收集整理的這篇文章主要介紹了
Spring5源码 - 14 如何在所有Bean创建完后做扩展?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 葛大爺的問題
- Answer
- 方式一 基于SmartInitializingSingleton接口
- Source
- Code
- 方式二 基于Spring事件監聽
- Source
- Code
- 驗證
葛大爺的問題
Answer
想要回答這個問題,就要對Spring的生命周期有一定的了解,今天我們就來回顧一下IOC的生命周期及Spring提供給開發人員的擴展點,當然了,我們今天只聊Bean加載完成后的事兒 。
老規矩 先應用后源碼 ,開搞~
AAA BBB CCC 均是spring管理的bean
@Component public class AAA {public AAA() {System.out.println("AAA init");} }方式一 基于SmartInitializingSingleton接口
Source
生命周期中倒數第二步
// Instantiate all remaining (non-lazy-init) singletons.finishBeanFactoryInitialization(beanFactory);SmartInitializingSingleton接口是在所有的Bean實例化完成以后,Spring回調的方法, 所以這里也是一個擴展點,可以在單例bean全部完成實例化以后做處理。
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 {}【擴展類 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 , 自定義擴展here ");} }【測試】
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事件監聽
Source
生命周期的最后一步是finishRefresh();,這里面中有一個方法是publishEvent
所以這里也可以進行擴展,監聽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("監聽到ContextRefreshedEvent, 自定義擴展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的監聽");} }二選一,推薦基于注解的方式
【測試】
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);}}驗證
總結
以上是生活随笔為你收集整理的Spring5源码 - 14 如何在所有Bean创建完后做扩展?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring5源码 - 13 Sprin
- 下一篇: Redis进阶 - Redis主从工作原