本文共 4532 字,大约阅读时间需要 15 分钟。
BeanPostProcessor
关于对象初始化前后的回调。
1 2 3 4 5 6 7 8 9 10 11 12 |
|
InstantiationAwareBeanPostProcessor
关于对象实例化前后以及实例化后设置propertyValues的回调
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
SmartInstantiationAwareBeanPostProcessor
这个接口主要是spring框架内部来使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
用来将merged BeanDefinition暴露出来的回调
1 2 3 4 5 |
|
关于处理对象销毁的前置回调
应用实例: ApplicationListenerDetector,这个类是用来注册ApplicationListener实例的,而如果销毁一个对象,不接触这里的引用会导致无法进行回收,因此在销毁对象时,会判断如果是ApplicationListener要执行从监听器列表中移除掉。 1 2 3 4 5 6 7 8 |
|
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);之前执行到 因此这三个方法没有严格的顺序意义转载地址:http://hbdoi.baihongyu.com/