日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring5源码 - 14 如何在所有Bean创建完后做扩展?

發布時間:2025/3/21 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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创建完后做扩展?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。