BeanPostProcessor是Spring框架中非常重要的bean之一。贯穿在Spring容器中bean的初始化的整个过程。
Spring中的beanpostProcess体系结构如下:

可以看到BeanPostProcessor的实现类还是蛮多的。

首先我们来看一下BeanPostProcessor的作用。

BeanPostProcessor接口有两个方法:
     @Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
} @Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

那么这两个方法的调用时机是在什么时候呢?

由一下代码可以看出

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

 

Spring中的beanPostProcess的作用的更多相关文章

  1. EnableAutoConfiguration注解 Spring中@Import注解的作用和使用

    EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...

  2. Spring中<context:annotation-config/>的作用

    spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Spring容 ...

  3. spring 中的断言的作用

    org.springframework.util.AssertAssert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了.就是断定某一个实际的值就为自己预期想得到的,如 ...

  4. cache在spring中使用

    一:参考文章 (1)http://haohaoxuexi.iteye.com/blog/2123030  Spring使用Cache,这篇文章讲的比较详细. 注:本文是对参考文章和实际使用中经验的总结 ...

  5. spring中scope(作用越)理解

    今天总结了一下spring中作用域scope的用法.在spring中作用域通过配置文件形式的用法如下. <bean id="role" class="spring. ...

  6. spring中的BeanFactory与ApplicationContext的作用和区别?

    BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...

  7. Spring中各个jar包的作用

    spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...

  8. Spring中事务配置以及事务不起作用可能出现的问题

    前言:在Spring中可以通过对方法进行事务的配置,而不是像原来通过手动写代码的方式实现事务的操作,这在很大程度上减少了开发的难度,本文介绍Spring事务配置的两种方式:基于配置文件的方式和基于注解 ...

  9. (转)web.xml中的contextConfigLocation在spring中的作用

    (转)web.xml中的contextConfigLocation在spring中的作用   一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...

随机推荐

  1. PHP 面试题 一

    1.用PHP打印出前一天的时间格式是2017-5-10 22:21:21(2分) 月,日没有前导零:2017-5-1 22:21:21echo date("Y-n-j H:i:s" ...

  2. Python--day23--初识面向对象复习

    面向对象编程是大程序编程思想:

  3. SAX解析xml (遍历DOM树各节点)

    本文参考 http://yangjunfeng.iteye.com/blog/401377 1. books.xml <?xml version="1.0" encoding ...

  4. 2019-8-15-win10-edge-打开闪退问题

    title author date CreateTime categories win10 edge 打开闪退问题 lindexi 2019-08-15 08:53:22 +0800 2019-8-1 ...

  5. Vue递归菜单

    一.效果图: 二.代码(Vue Cli 快速原型开发) App.vue <template> <div id="app"> <template v-f ...

  6. python基础数据类型汇总

    list和dict 在循环一个列表和字典时,最好不要删除其中的元素,这样会使索引发生改变,从而报错! lis = [11, 22, 33, 44, 55] for i in range(len(lis ...

  7. sublime text3神器插件

    (首先安装)Package Control  Package Control,就像 Linux 下的 apt-get 和 yum 一样,它是 Sublime Text 的包管理器 1.Emmet 是一 ...

  8. P1091 剧院广场

    题目描述 柏林首都的剧院广场呈长方形,面积为 \(n \times m\) 平方米.在这座城市的周年纪念日之际,人们决定用方形花岗岩石板铺设广场.每块石板的大小都是 \(a \times a\) . ...

  9. vue-learning:41 - Vuex - 第二篇:const store = new Vue.Store(option)中option选项、store实例对象的属性和方法

    vuex 第二篇:const store = new Vue.Store(option)中option选项.store实例对象的属性和方法 import Vuex from 'vuex' const ...

  10. webpack4.0基本配置,超简单!

    最近复习了一下webpack,使用的是4.0版本. 下图是基本目录结构,最后留有代码地址,有兴趣可以去看看. 直接上代码(依赖未完全使用): 项目的所有依赖都可以安装,每个都有详细的注释.] cons ...