BeanPostProcessor

关于对象初始化前后的回调。

public interface BeanPostProcessor {
//该方法在bean实例化完毕(且已经注入完毕),在afterPropertiesSet或自定义init方法执行之前
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
//在afterPropertiesSet或自定义init方法执行之后
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}

InstantiationAwareBeanPostProcessor

关于对象实例化前后以及实例化后设置propertyValues的回调

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
//这个方法用来在对象实例化前直接返回一个对象(如代理对象)来代替通过内置的实例化流程创建对象;
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
//在对象实例化完毕执行populateBean之前 如果返回false则spring不再对对应的bean实例进行自动依赖注入。
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
//这里是在spring处理完默认的成员属性,应用到指定的bean之前进行回调,可以用来检查和修改属性,最终返回的PropertyValues会应用到bean中
//@Autowired、@Resource等就是根据这个回调来实现最终注入依赖的属性的。
@Nullable
default PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
return pvs;
}
}

 SmartInstantiationAwareBeanPostProcessor

这个接口主要是spring框架内部来使用

public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {
//用来返回目标对象的类型(比如代理对象通过raw class获取proxy type 用于类型匹配)
@Nullable
default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
//这里提供一个拓展点用来解析获取用来实例化的构造器(比如未通过bean定义构造器以及参数的情况下,会根据这个回调来确定构造器)
@Nullable
default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
throws BeansException {
return null;
}
//获取要提前暴露的bean的引用,用来支持单例对象的循环引用(一般是bean自身,如果是代理对象则需要取用代理引用)
default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
return bean;
}
}

MergedBeanDefinitionPostProcessor

用来将merged BeanDefinition暴露出来的回调

public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {
//在bean实例化完毕后调用 可以用来修改merged BeanDefinition的一些properties 或者用来给后续回调中缓存一些meta信息使用
//这个算是将merged BeanDefinition暴露出来的一个回调
void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
}

DestructionAwareBeanPostProcessor

关于处理对象销毁的前置回调
应用实例:
    ApplicationListenerDetector,这个类是用来注册ApplicationListener实例的,而如果销毁一个对象,不接触这里的引用
    会导致无法进行回收,因此在销毁对象时,会判断如果是ApplicationListener要执行从监听器列表中移除掉。

public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
//这里实现销毁对象的逻辑
void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
//判断是否需要处理这个对象的销毁
default boolean requiresDestruction(Object bean) {
return true;
}
}

关于BeanPostProcessor中各个回调调用的顺序

1、InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(beanClass, beanName)
    该方法在创建对象之前会先掉用,如果有返回实例则直接使用不会去走下面创建对象的逻辑,并在之后执行
        BeanPostProcessor.postProcessAfterInitialization(result, beanName)
2、SmartInstantiationAwareBeanPostProcessor.determineCandidateConstructors(beanClass, beanName)
    如果需要的话,会在实例化对象之前执行
3、MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition(mbd, beanType, beanName)
    在对象实例化完毕 初始化之前执行
4、InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)
    在bean创建完毕初始化之前执行
5、InstantiationAwareBeanPostProcessor.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName)
    在bean的property属性注入完毕 向bean中设置属性之前执行
6、BeanPostProcessor.postProcessBeforeInitialization(result, beanName)
    在bean初始化(自定义init或者是实现了InitializingBean.afterPropertiesSet())之前执行
7、BeanPostProcessor.postProcessAfterInitialization(result, beanName)
    在bean初始化(自定义init或者是实现了InitializingBean.afterPropertiesSet())之后执行
8、其中DestructionAwareBeanPostProcessor方法的postProcessBeforeDestruction(Object bean, String beanName)会在销毁对象前执行

DestructionAwareBeanPostProcessor 中的requiresDestruction(Object bean)是用来判断是否属于当前processor处理的bean
SmartInstantiationAwareBeanPostProcessor中的predictBeanType(Class<?> beanClass, String beanName)是用来预判类型的
SmartInstantiationAwareBeanPostProcessor.getEarlyBeanReference(exposedObject, beanName)
    这个方法仅仅是在这一步是作为一个ObjectFactory封装起来放到singletonFactories中的,
    仅在并发情况下 刚好在当前对象设置进去,而另一个bean创建需要getBean获取时才会立即执行
    因此这一步的顺序是不一定的,有可能永远不会执行(无并发循坏依赖对象创建的场景)
    可能在3之后对象实例化完毕执行addSingleton(beanName, singletonObject);之前执行到
因此这三个方法没有严格的顺序意义

BeanPostProcessor的五大接口的更多相关文章

  1. hibernate的五大接口

    Hibernate有五大核心接口,分别是:Session Transaction Query SessionFactoryConfiguration .这五个接口构成了Hibernate运行的基本要素 ...

  2. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  3. 谈谈Spring中的BeanPostProcessor接口

    一.前言   这几天正在复习Spring的相关内容,在了解bean的生命周期的时候,发现其中涉及到一个特殊的接口--BeanPostProcessor接口.由于网上没有找到比较好的博客,所有最后花了好 ...

  4. spring源码:BeanPostProcessor(li)

    在spring管理Bean的初始化过程中,除了正常管理bean的实例化(初始化.参数注入等)外,还对外提供了丰富的对Bean操作的扩展.例如自定义初始化操作,自定义容器退出时Bean的销毁操作等等.这 ...

  5. spring中基础核心接口总结

    spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...

  6. Spring之InstantiationAwareBeanPostProcessor接口介绍

      InstantiationAwareBeanPostProcessor接口是BeanPostProcessor的子接口,通过接口字面意思翻译该接口的作用是感知Bean实例话的处理器.实际上该接口的 ...

  7. spring扩展点之一:BeanFactoryPostProcessor和BeanPostProcessor

    一.BeanFactoryPostProcessor和BeanPostProcessor的区别 BeanFactoryPostProcessor和BeanPostProcessor都是spring初始 ...

  8. 【原】通过Spring结合Cglib处理非接口代理

    前言: 之前做的一个项目,虽然是查询ES,但内部有大量的逻辑计算,非常耗时,而且经常收到JVM峰值告警邮件.分析了一下基础数据每天凌晨更新一次,但查询和计算其实在第一次之后就可以写入缓存,这样后面直接 ...

  9. 简单分析BeanPostProcessor

    1. 什么是BeanPostProcessorBeanPostProcessor是一个接口,有两个方法,分别是:Object postProcessBeforeInitialization(Objec ...

随机推荐

  1. 微信小程序组件 加减号弹出框

    <!-- 点击立即抢拼弹出框 --> <view class='add-rob' bindtap="setModalStatus" data-status=&qu ...

  2. Vue2.0 render:h => h(App)

    new Vue({ router, store, //components: { App } vue1.0的写法 render: h => h(App) vue2.0的写法 }).$mount( ...

  3. Java知识点整理(二)

    List.Set.Map典型实现 HashMap/ConcurrentHashMap Java线程池 Java线程池详解 如何更好的使用JAVA线程池 Spring MVC Spring MVC架构浅 ...

  4. 【刷题】BZOJ 4650 [Noi2016]优秀的拆分

    Description 如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串,则我们称该字符串的这种拆分是优秀的.例如,对于字符串 aabaabaa,如果令 A ...

  5. [AT2557] [arc073_c] Ball Coloring

    题目链接 AtCoder:https://arc073.contest.atcoder.jp/tasks/arc073_c 洛谷:https://www.luogu.org/problemnew/sh ...

  6. 我是一个CPU:这个世界慢!死!了!

    最近小编看到一篇十分有意思的文章,多方位.无死角的讲解了CPU关于处理速度的理解,看完之后真是豁然开朗.IOT时代,随着科技的发展CPU芯片的处理能力越来越强,强大的程度已经超乎了我们的想象.今天就把 ...

  7. 初识python版本

    区别一: python2x:源码重复不规范. python3x:重新整理规范了源码. 区别二: python2x: 默认的编码方式ascii,显示中文需要首行添加:#  _*_ encoding: u ...

  8. Myeclipse下更改所有jsp、html文件的编码

    windows-->>preferences-->>gengral-->>

  9. Codeforces Round #407 (Div. 2)A B C 水 暴力 最大子序列和

    A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...

  10. 简单shell 编程

    简单shell编程  by  dreamboy #!/bin/bash while true do echo clear echo echo " 系统维护菜单 " echo &qu ...