前言:现今SpringBoot、SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解、原理,比如@Conditional、@Import、@EnableXXX等。如果掌握这些底层原理、注解,那么我们对这些高层框架就能做到高度定制,使用的游刃有余

一、BeanFactoryPostProcessor

  BeanPostProcessor是bean后置处理器,bean创建对象初始化前后进行拦截工作的,而BeanFactoryPostProcessor是beanFactory的后置处理器在BeanFactory标准初始化之后调用,来定制和修改BeanFactory的内容所有的bean定义已经保存加载到beanFactory,但是bean的实例还未创建;

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanFactoryPostProcessor...postProcessBeanFactory...");
int count = beanFactory.getBeanDefinitionCount();
String[] names = beanFactory.getBeanDefinitionNames();
System.out.println("当前BeanFactory中有"+count+" 个Bean");
System.out.println(Arrays.asList(names));
}
}

BeanFactoryPostProcessor原理:  

  1)、ioc容器创建对象

  2)、invokeBeanFactoryPostProcessors(beanFactory)

    1)、直接在BeanFactory中找到所有类型是BeanFactoryPostProcessor的组件,并执行他们的方法

    2)、在初始化创建其他组件前面执行

二、BeanDefinitionRegistryPostProcessor

  BeanDefinitionRegistryPostProcessor 继承于 BeanFactoryPostProcessor,postProcessBeanDefinitionRegistry()方法:

    1)、在所有bean定义信息将要被加载,bean实例还未创建的;

    2)、优先于BeanFactoryPostProcessor执行;

    3)、利用BeanDefinitionRegistryPostProcessor给容器中再额外添加一些组件

@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor{ @Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
System.out.println("MyBeanDefinitionRegistryPostProcessor...bean的数量:"+beanFactory.getBeanDefinitionCount());
} //BeanDefinitionRegistry Bean定义信息的保存中心,以后BeanFactory就是按照BeanDefinitionRegistry里面保存的每一个bean定义信息创建bean实例;
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeanDefinitionRegistry...bean的数量:"+registry.getBeanDefinitionCount());
//RootBeanDefinition beanDefinition = new RootBeanDefinition(Blue.class);
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();
registry.registerBeanDefinition("hello", beanDefinition);
} }
    

原理:
  1)、ioc创建对象

  2)、refresh()-》invokeBeanFactoryPostProcessors(beanFactory)

  3)、从容器中获取到所有的BeanDefinitionRegistryPostProcessor组件

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

    2、再来触发postProcessBeanFactory()方法BeanFactoryPostProcessor

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

三、ApplicationListener 

1.使用:

@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> { //当容器中发布此事件以后,方法触发
@Override
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
System.out.println("收到事件:"+event);
}
}
@Service
public class UserService { @EventListener(classes={ApplicationEvent.class})
public void listen(ApplicationEvent event){
System.out.println("UserService。。监听到的事件:"+event);
} }

通过实现ApplicationListener(在方法上标注@EventListener)来监听容器中发布的事件(ApplicationEvent 及其下面的子事件):

   ContextRefreshedEvent:容器刷新完成(所有bean都完全创建)会发布这个事件

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

  发布一个事件:applicationContext.publishEvent()

2、原理:

  1)、ContextRefreshedEvent事件:
    1)、容器创建对象:refresh();
    2)、finishRefresh();容器刷新完成会发布ContextRefreshedEvent事件
  2)、自己发布事件;
  3)、容器关闭会发布ContextClosedEvent;

【事件发布流程】:
  4)、publishEvent(new ContextRefreshedEvent(this));
    1)、获取事件的多播器(派发器):getApplicationEventMulticaster()
    2)、multicastEvent派发事件:
    3)、获取到所有的ApplicationListener;
      for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
      1)、如果有Executor,可以支持使用Executor进行异步派发;
        Executor executor = getTaskExecutor();
      2)、否则,同步的方式直接执行listener方法;invokeListener(listener, event);
        拿到listener回调onApplicationEvent方法;

【事件多播器(派发器)】
  1)、容器创建对象:refresh();
  2)、initApplicationEventMulticaster();初始化ApplicationEventMulticaster;
    1)、先去容器中找有没有id=“applicationEventMulticaster”的组件;
    2)、如果没有this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
      并且加入到容器中,我们就可以在其他组件要派发事件,自动注入这个applicationEventMulticaster;

【容器中有哪些监听器】
  1)、容器创建对象:refresh();
  2)、registerListeners();
    从容器中拿到所有的监听器,把他们注册到applicationEventMulticaster中;  
    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
    //将listener注册到ApplicationEventMulticaster中
    getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);

@EventListener原理:使用EventListenerMethodProcessor处理器来解析方法上的@EventListener:

  EventListenerMethodProcessor实现了SmartInitializingSingleton,afterSingletonsInstantiated()执行原理:

    1)、ioc容器创建对象并refresh()

    2)、finishBeanFactoryInitialization(beanFactory);初始化剩下的单实例bean

      1)、先创建所有的单实例bean;getBean()

      2)、获取所有创建好的单实例bean,判断是否是SmartInitializingSingleton类型的,如果是就调用afterSingletonsInstantiated()

  

Spring注解驱动开发之扩展原理的更多相关文章

  1. 【spring 注解驱动开发】扩展原理

    尚学堂spring 注解驱动开发学习笔记之 - 扩展原理 扩展原理 1.扩展原理-BeanFactoryPostProcessor BeanFactoryPostProcessor * 扩展原理: * ...

  2. Spring注解驱动开发(五)-----扩展原理

    扩展原理 1.BeanPostProcessor-----bean后置处理器,bean创建对象初始化前后进行拦截工作的 2.BeanFactoryPostProcessor-----beanFacto ...

  3. 【spring 注解驱动开发】Spring AOP原理

    尚学堂spring 注解驱动开发学习笔记之 - AOP原理 AOP原理: 1.AOP原理-AOP功能实现 2.AOP原理-@EnableAspectJAutoProxy 3.AOP原理-Annotat ...

  4. 【spring 注解驱动开发】spring事务处理原理

    尚学堂spring 注解驱动开发学习笔记之 - 事务处理 事务处理 1.事务处理实现 实现步骤: * 声明式事务: * * 环境搭建: * 1.导入相关依赖 * 数据源.数据库驱动.Spring-jd ...

  5. 【spring 注解驱动开发】spring ioc 原理

    尚学堂spring 注解驱动开发学习笔记之 - Spring容器创建 Spring容器创建 1.Spring容器创建-BeanFactory预准备 2.Spring容器创建-执行BeanFactory ...

  6. 0、Spring 注解驱动开发

    0.Spring注解驱动开发 0.1 简介 <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作 ...

  7. 【Spring注解驱动开发】聊聊Spring注解驱动开发那些事儿!

    写在前面 今天,面了一个工作5年的小伙伴,面试结果不理想啊!也不是我说,工作5年了,问多线程的知识:就只知道继承Thread类和实现Runnable接口!问Java集合,竟然说HashMap是线程安全 ...

  8. 【Spring注解驱动开发】关于BeanPostProcessor后置处理器,你了解多少?

    写在前面 有些小伙伴问我,学习Spring是不是不用学习到这么细节的程度啊?感觉这些细节的部分在实际工作中使用不到啊,我到底需不需要学习到这么细节的程度呢?我的答案是:有必要学习到这么细节的程度,而且 ...

  9. 【spring 注解驱动开发】spring对象的生命周期

    尚学堂spring 注解驱动开发学习笔记之 - 生命周期 生命周期 1.生命周期-@Bean指定初始化和销毁方法 2.生命周期-InitializingBean和DisposableBean 3.生命 ...

随机推荐

  1. 简单JS全选、反选代码

    1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org ...

  2. STM32 USB复合设备编写

    目的 完成一个CDC + MSC的复合USB设备 可以方便在CDC,MSC,复合设备三者间切换 可移植性强 预备知识 cube中USB只有两个入口. main函数中的MX_USB_DEVICE_Ini ...

  3. 鸟哥的Linux私房菜-----12、学习使用Shell scripts

  4. 2016/4/17 去除 ul ol 前标记 list-style:none list-style-type:none

    对于很多人用div来做网站时,总会用到,但在显示效果时前面总是会有一个小黑点,这个令很多人头痛,但又找不到要源,其它我们可以用以下方法来清除. 1.在CSS中写入代码.找到相关性的CSS,在..li和 ...

  5. 调参侠的末日? Auto-Keras 自动搜索深度学习模型的网络架构和超参数

    Auto-Keras 是一个开源的自动机器学习库.Auto-Keras 的终极目标是允许所有领域的只需要很少的数据科学或者机器学习背景的专家都可以很容易的使用深度学习.Auto-Keras 提供了一系 ...

  6. eclispe pydev tab改回 空格找到方法了,这个链接:http://stackoverflow.com/questions/23570925/eclipse-indents-new-line-with-tabs-instead-of-spaces

    看这个链接: 3down votefavorite 1 I've followed all the suggestions here. When I press return, I get a new ...

  7. hashable

    Glossary — Python 3.6.5 documentation https://docs.python.org/3/glossary.html?highlight=equal hashab ...

  8. 【Android进度条】三种方式实现自定义圆形进度条ProgressBar

    一.通过动画实现 定义res/anim/loading.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...

  9. 并不对劲的bzoj3277

    陈年老坑 题意大概是有n个字符串,要求出每一个字符串的所有子串(不包括空串)在所有字符串(包括自身)中出现次数不少于k的有多少个.n,k,字符串总长<=100000. 如果只有一个串的话,非常好 ...

  10. BZOJ_2947_[Poi2000]促销_堆

    BZOJ_2947_[Poi2000]促销_堆 Description Bytelandish连锁超市委托你编写一个程序来模拟一项即将施行的促销活动,该活动的规则如下: ●想要参与的顾客,只需把他的个 ...