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));

  }无锡人流医院哪家好 http://mobile.wxbhnkyy120.com/

  }

  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();

  }

  }

posted @ 2019-07-17 13:48  tiana_Z  阅读(196)  评论(0编辑  收藏  举报