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

 

 

 



 

 




posted on 2020-01-13 23:48  城序猿  阅读(6378)  评论(0编辑  收藏  举报