Spring中的beanPostProcess的作用


BeanPostProcessor是Spring框架中非常重要的bean之一。貫穿在Spring容器中bean的初始化的整個過程。

Spring中的beanpostProcess體系結構如下:

 

 

 

可以看到BeanPostProcessor的實現類還是蠻多的。

首先我們來看一下BeanPostProcessor的作用。

BeanPostProcessor接口有兩個方法:
 1     @Nullable
 2     default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 3         return bean;
 4     }
 5 
 6 
 7 @Nullable
 8     default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 9         return bean;
10     }

  那么這兩個方法的調用時機是在什么時候呢?

 由一下代碼可以看出

postProcessBeforeInitialization 是在屬性注入populateBean方法之后,在initializeBean中調用。
通過
applyBeanPostProcessorsBeforeInitialization 方法獲取到容器中所有的beanpostprocessor 類型的實例, 然后調用其postProcessBeforeInitialization 方法
通過applyBeanPostProcessorsAfterInitialization 方法獲取到容器中所有的beanpostprocessor 類型的實例, 然后調用其postProcessAfterInitialization方法
兩個方法之間的邏輯是調用invokeInitMethods,調用實現了InitializingBean 的 afterpropertiesSet方法,和執行 執行init-method方法。
 
         
//ioc di,依賴注入的核心方法,該方法必須看,重要程度:5
populateBean(beanName, mbd, instanceWrapper);

//bean 實例化+ioc依賴注入完以后的調用,非常重要,重要程度:5
exposedObject = initializeBean(beanName, exposedObject, mbd);
// 具體的initializalizeBean的實現邏輯
protected
Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareMethods(beanName, bean); return null; }, getAccessControlContext()); } else { //調用Aware方法 invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { //對類中某些特殊方法的調用,比如@PostConstruct,Aware接口,非常重要 重要程度 :5 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { //InitializingBean接口,afterPropertiesSet,init-method屬性調用,非常重要,重要程度:5 invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { //這個地方可能生出代理實例,是aop的入口 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }

// 具體的
applyBeanPostProcessorsBeforeInitialization 實現邏輯
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {

Object result = existingBean;
/*
* 着重看幾個
* 1、ApplicationContextAwareProcessor 對某個Aware接口方法的調用
* 2、InitDestroyAnnotationBeanPostProcessor @PostConstruct注解方法的調用
*
* 3、ImportAwareBeanPostProcessor 對ImportAware類型實例setImportMetadata調用
* 這個對理解springboot有很大幫助。 這里暫時不需要深入看
* */
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}

// 具體的applyBeanPostProcessorsAfterInitialization實現邏輯
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {

Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}

 

 

 



 

 





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM