Spring注解驅(qū)動擴(kuò)展原理BeanFactoryPostProcessor
1、擴(kuò)展原理-BeanFactoryPostProcessor
BeanFactoryPostProcessor
* 擴(kuò)展原理:
* BeanPostProcessor:bean后置處理器,bean創(chuàng)建對象初始化前后進(jìn)行攔截工作的
*
* 1、BeanFactoryPostProcessor:beanFactory的后置處理器;
* 在BeanFactory標(biāo)準(zhǔn)初始化之后調(diào)用,來定制和修改BeanFactory的內(nèi)容;
* 所有的bean定義已經(jīng)保存加載到beanFactory,但是bean的實(shí)例還未創(chuàng)建
*
*
* BeanFactoryPostProcessor原理:
* 1)、ioc容器創(chuàng)建對象
* 2)、invokeBeanFactoryPostProcessors(beanFactory);
* 如何找到所有的BeanFactoryPostProcessor并執(zhí)行他們的方法;
* 1)、直接在BeanFactory中找到所有類型是BeanFactoryPostProcessor的組件,并執(zhí)行他們的方法
* 2)、在初始化創(chuàng)建其他組件前面執(zhí)行
代碼實(shí)現(xiàn)
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanFactoryPostProcessor...postProcessBeanFactory...");
int count = beanFactory.getBeanDefinitionCount();
String[] names = beanFactory.getBeanDefinitionNames();
System.out.println("當(dāng)前BeanFactory中有"+count+" 個Bean");
System.out.println(Arrays.asList(names));
}
}
2、擴(kuò)展原理-BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor
* 2、BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor
* postProcessBeanDefinitionRegistry();
* 在所有bean定義信息將要被加載,bean實(shí)例還未創(chuàng)建的;
*
* 優(yōu)先于BeanFactoryPostProcessor執(zhí)行;
* 利用BeanDefinitionRegistryPostProcessor給容器中再額外添加一些組件;
*
* 原理:
* 1)、ioc創(chuàng)建對象
* 2)、refresh()-》invokeBeanFactoryPostProcessors(beanFactory);
* 3)、從容器中獲取到所有的BeanDefinitionRegistryPostProcessor組件。
* 1、依次觸發(fā)所有的postProcessBeanDefinitionRegistry()方法
* 2、再來觸發(fā)postProcessBeanFactory()方法BeanFactoryPostProcessor;
*
* 4)、再來從容器中找到BeanFactoryPostProcessor組件;然后依次觸發(fā)postProcessBeanFactory()方法
代碼實(shí)現(xiàn)
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor{
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
System.out.println("MyBeanDefinitionRegistryPostProcessor...bean的數(shù)量:"+beanFactory.getBeanDefinitionCount());
}
//BeanDefinitionRegistry Bean定義信息的保存中心,以后BeanFactory就是按照BeanDefinitionRegistry里面保存的每一個bean定義信息創(chuàng)建bean實(shí)例;
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeanDefinitionRegistry...bean的數(shù)量:"+registry.getBeanDefinitionCount());
//RootBeanDefinition beanDefinition = new RootBeanDefinition(Blue.class);
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();
registry.registerBeanDefinition("hello", beanDefinition);
}
}
3、擴(kuò)展原理-ApplicationListener用法
監(jiān)聽器ApplicationListener
* 3、ApplicationListener:監(jiān)聽容器中發(fā)布的事件。事件驅(qū)動模型開發(fā);
* public interface ApplicationListener<E extends ApplicationEvent>
* 監(jiān)聽 ApplicationEvent 及其下面的子事件;
*
* 步驟:
* 1)、寫一個監(jiān)聽器(ApplicationListener實(shí)現(xiàn)類)來監(jiān)聽某個事件(ApplicationEvent及其子類)
* @EventListener;
* 原理:使用EventListenerMethodProcessor處理器來解析方法上的@EventListener;
*
* 2)、把監(jiān)聽器加入到容器;
* 3)、只要容器中有相關(guān)事件的發(fā)布,我們就能監(jiān)聽到這個事件;
* ContextRefreshedEvent:容器刷新完成(所有bean都完全創(chuàng)建)會發(fā)布這個事件;
* ContextClosedEvent:關(guān)閉容器會發(fā)布這個事件;
* 4)、發(fā)布一個事件:
* applicationContext.publishEvent();
代碼實(shí)現(xiàn):
方式一:實(shí)現(xiàn)ApplicationListener<E extends ApplicationEvent>接口
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
//當(dāng)容器中發(fā)布此事件以后,方法觸發(fā)
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
System.out.println("收到事件:"+event);
}
}
方式二:使用@EventListener注解標(biāo)識事件監(jiān)聽方法
@Service
public class UserService {
@EventListener(classes={ApplicationEvent.class})
public void listen(ApplicationEvent event){
System.out.println("UserService。。監(jiān)聽到的事件:"+event);
}
}
4、擴(kuò)展原理-ApplicationListener原理
* 原理:
* ContextRefreshedEvent、IOCTest_Ext$1[source=我發(fā)布的時間]、ContextClosedEvent;
* 1)、ContextRefreshedEvent事件:
* 1)、容器創(chuàng)建對象:refresh();
* 2)、finishRefresh();容器刷新完成會發(fā)布ContextRefreshedEvent事件
* 2)、自己發(fā)布事件;
* 3)、容器關(guān)閉會發(fā)布ContextClosedEvent;
*
* 【事件發(fā)布流程】:
* 3)、publishEvent(new ContextRefreshedEvent(this));
* 1)、獲取事件的多播器(派發(fā)器):getApplicationEventMulticaster()
* 2)、multicastEvent派發(fā)事件:
* 3)、獲取到所有的ApplicationListener;
* for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
* 1)、如果有Executor,可以支持使用Executor進(jìn)行異步派發(fā);
* Executor executor = getTaskExecutor();
* 2)、否則,同步的方式直接執(zhí)行l(wèi)istener方法;invokeListener(listener, event);
* 拿到listener回調(diào)onApplicationEvent方法;
*
* 【事件多播器(派發(fā)器)】
* 1)、容器創(chuàng)建對象:refresh();
* 2)、initApplicationEventMulticaster();初始化ApplicationEventMulticaster;
* 1)、先去容器中找有沒有id=“applicationEventMulticaster”的組件;
* 2)、如果沒有this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
* 并且加入到容器中,我們就可以在其他組件要派發(fā)事件,自動注入這個applicationEventMulticaster;
*
* 【容器中有哪些監(jiān)聽器】
* 1)、容器創(chuàng)建對象:refresh();
* 2)、注冊監(jiān)聽器:registerListeners();
* 從容器中拿到所有的監(jiān)聽器,把他們注冊到applicationEventMulticaster中;
* String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
* //將listener注冊到ApplicationEventMulticaster中
* getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
5、擴(kuò)展原理-@EventListener與SmartInitializingSingleton
* SmartInitializingSingleton 原理:->afterSingletonsInstantiated();
* 1)、ioc容器創(chuàng)建對象并refresh();
* 2)、finishBeanFactoryInitialization(beanFactory);初始化剩下的單實(shí)例bean;
* 1)、先創(chuàng)建所有的單實(shí)例bean;getBean();
* 2)、獲取所有創(chuàng)建好的單實(shí)例bean,判斷是否是SmartInitializingSingleton類型的;
* 如果是就調(diào)用afterSingletonsInstantiated();
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決springboot利用ConfigurationProperties注解配置數(shù)據(jù)源無法讀取配置信息問題
今天在學(xué)習(xí)springboot利用ConfigurationProperties注解配置數(shù)據(jù)源的使用遇到一個問題無法讀取配置信息,發(fā)現(xiàn)全部為null,糾結(jié)是哪里出了問題呢,今天一番思考,問題根源找到,下面把我的解決方案分享到腳本之家平臺,感興趣的朋友一起看看吧2021-05-05
Java Kafka 消費(fèi)積壓監(jiān)控的示例代碼
這篇文章主要介紹了Java Kafka 消費(fèi)積壓監(jiān)控,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
Spring Boot整合logback一個簡單的日志集成架構(gòu)
今天小編就為大家分享一篇關(guān)于Spring Boot整合logback一個簡單的日志集成架構(gòu),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
解決Springboot項(xiàng)目啟動后自動創(chuàng)建多表關(guān)聯(lián)的數(shù)據(jù)庫與表的方案
這篇文章主要介紹了解決Springboot項(xiàng)目啟動后自動創(chuàng)建多表關(guān)聯(lián)的數(shù)據(jù)庫與表的方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

