APP下载
报价宝  ›  科技  › 

BeanPostProcessor后置处理器原理以及ApplicationListener原理

报价宝 来源:baojiabao.com 发布时间:2019-07-30 17:48:00 10月06日更新
报价宝综合消息BeanPostProcessor后置处理器原理以及ApplicationListener原理

BeanPostProcessor:bean后置处理器,bean建立物件初始化前后进行拦截工作的

1、BeanFactoryPostProcessor:BeanFactory的后置处理器;

在BeanFactory标准初始化之后呼叫;所有的bean定义已经储存载入到BeanFactory,但是bean的例项还没建立;

BeanFactoryPostProcessor原理:

1.1 ioc容器建立物件

1.2 invokeBeanFactoryPostProcessors(BeanFactory); 执行BeanFactoryPostProcessor;

如何找到所有的BeanFactoryPostProcessor并执行他们的方法;

1.2.1 直接在BeanFactory中找到所有型别是BeanFactoryPostProcessor的元件,并执行他们的方法

1.2.2 在初始化建立其他元件前面执行

2、BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor

postProcessBeanDefinitionRegistry();

在所有bean定义资讯将要被载入,bean例项还未建立的;

优先于BeanFactoryPostProcessor执行;

利用BeanDefinitionRegistryPostProcessor给容器中在额外新增一些元件;

原理:

2.1、ioc建立物件

2.2、refresh()->invokeBeanFactoryPostProcessors(beanFactory);

2.3、从容器中获取到的BeanDefinitionRegistryPostProcessor元件。

2.3.1 依次触发所有的postProcessBeanDefinitionRegistry()方法

2.3.2 再来触发postProcessBeanFactory()方法BeanFactoryPostProcessor;

2.4、再来从容器中找到BeanFactoryPostProcessor元件;然后依次触发postProcessBeanFactory()方法

3、ApplicationListener:监听容器中释出的事件。事件驱动模型开发;

public interface ApplicationListener

监听ApplicationEvent 及其下面的子事件;

步骤:

1、写一个监听器来监听某个事件(ApplicationEvent及其子类)

2、把监听器加入到容器;

3、只要容器中有相关事件的释出,我们就能监听到这个事件;

ContextRefreshedEvent:容器重新整理完成(所有bean都完全建立)会发布这个事件;

ContextClosedEvent:关闭容器会发布这个事件;

4、释出一个事件;

applicationContext.publishEvent();

原理:

ContextRefreshedEvent、IOCTest_Ext$1[source=自己的事件!!]、ContextClosedEvent;

1、ContextRefreshedEvent事件:

1.1 容器建立物件:refresh();

1.2 finishRefresh();容器重新整理完成会发布ContextRefreshedEvent

1.3 publishEvent(new ContextRefreshedEvent(this));【事件释出流程】

1.3.1 获取事件的多波器(派发器):getApplicationEventMulticaster()

1.3.2 MulticasterEvent派发事件:

1.3.3 获取到所有的ApplicationListener

for(final ApplicationListener> listener : getApplicationListeners(event, type))

1.3.3.1 如果有Executor,可以支援使用Executor进行异步派发;

Executor executor = getTaskExecutor();

1.3.3.2 否则,同步的方式直接执行listener方法;invokeListener(listener, event);

拿到listener回拨onApplicationEvent方法;

2、释出自己的事件;

3、容器关闭会发布ContextClosedEvent

【事件多波器(派发器)】

1、容器建立物件:refresh();

2、initApplicationEventMulticaster();初始化ApplicationEventMulticaster;

2.1 先去容器中找到有没有id="applicationEventMultcaster"的元件;

2.2 如果没有this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);

并且加入到容器中,我们就可以在其他元件要派发事件,自动注入这个applicationEventMulticaster;

【容器中有哪些监听器】

1、容器建立物件:refresh();

2、registerListeners();

String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);

//将listener注册到ApplicationEventMulticaster中

getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);

package com.spring.ext;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import com.spring.bean.Red;

@ComponentScan("com.spring.ext")

@Configuration

public class ExtConfig {

@Bean

public Red blue() {

return new Red();

}

}

package com.spring.ext;

import org.springframework.context.ApplicationEvent;

import org.springframework.context.ApplicationListener;

import org.springframework.stereotype.Component;

@Component

public class MyApplicationListener implements ApplicationListener{

public void onApplicationEvent(ApplicationEvent event) {

System.out.println("event>>>>>>>>"+event);

}

}

package com.spring.ext;

import java.util.Arrays;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanFactoryPostProcessor;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import org.springframework.stereotype.Component;

@Component

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

System.out.println("MyBeanFactoryPostProcessor ... postProcessBeanFactory beansize="+beanFactory.getBeanDefinitionCount());

int count = beanFactory.getBeanDefinitionCount();

String[] names = beanFactory.getBeanDefinitionNames();

System.out.println("当前BeanFactory中有"+count+"个Bean");

System.out.println(Arrays.toString(names));

}

}

package com.spring.ext;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import org.springframework.beans.factory.support.AbstractBeanDefinition;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;

import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

import org.springframework.stereotype.Component;

import com.spring.bean.Red;

@Component

public class MyBenaDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

System.out.println("MyBenaDefinitionRegistryPostProcessor ..beansize ="+beanFactory.getBeanDefinitionCount());

}

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

System.out.println(">>>>>postProcessBeanDefinitionRegistry>>>beansize="+registry.getBeanDefinitionCount());

AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Red.class).getBeanDefinition();

registry.registerBeanDefinition("hello", beanDefinition);

}

}

package com.spring.ext;

import org.springframework.context.ApplicationEvent;

import org.springframework.context.event.EventListener;

import org.springframework.stereotype.Service;

@Service

public class UserService {

@EventListener(classes= {ApplicationEvent.class})

public void listener(ApplicationEvent event) {

System.out.println("自己监听>>>>>>"+event);

}

}

package com.spring.test;

import org.junit.Test;

import org.springframework.context.ApplicationEvent;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.spring.ext.ExtConfig;

public class IOCTestOfExt {

@Test

public void test01() {

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ExtConfig.class);

applicationContext.publishEvent(new ApplicationEvent("自己的事件!!") {

});

applicationContext.close();

}

}

文章标签: 报价宝 降噪耳机价格 耳机价格 红米手机价格 华为手机价格 小米手机价格 电视机价格 笔记本电脑价格 笔记本价格 汽车价格 数码相机价格 手机价格 红米手机价格 耳机价格 降噪耳机价格