Spring中的beanPostProcess的作用
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的作用的更多相关文章
- EnableAutoConfiguration注解 Spring中@Import注解的作用和使用
EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...
- Spring中<context:annotation-config/>的作用
spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Spring容 ...
- spring 中的断言的作用
org.springframework.util.AssertAssert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了.就是断定某一个实际的值就为自己预期想得到的,如 ...
- cache在spring中使用
一:参考文章 (1)http://haohaoxuexi.iteye.com/blog/2123030 Spring使用Cache,这篇文章讲的比较详细. 注:本文是对参考文章和实际使用中经验的总结 ...
- spring中scope(作用越)理解
今天总结了一下spring中作用域scope的用法.在spring中作用域通过配置文件形式的用法如下. <bean id="role" class="spring. ...
- spring中的BeanFactory与ApplicationContext的作用和区别?
BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...
- Spring中各个jar包的作用
spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...
- Spring中事务配置以及事务不起作用可能出现的问题
前言:在Spring中可以通过对方法进行事务的配置,而不是像原来通过手动写代码的方式实现事务的操作,这在很大程度上减少了开发的难度,本文介绍Spring事务配置的两种方式:基于配置文件的方式和基于注解 ...
- (转)web.xml中的contextConfigLocation在spring中的作用
(转)web.xml中的contextConfigLocation在spring中的作用 一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...
随机推荐
- 最短路径Dijkstra算法和Floyd算法整理、
转载自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最短路径—Dijkstra算法和Floyd算法 Dijks ...
- js(三) ajax异步局部刷新技术底层代码实现
ajax 异步 javaScript and xml 开发五步骤: 1. 创建对象 XMLHttpRequest(chrome,firefox) ie... jquery 2. 找到连接, http的 ...
- webpack学习(五)entry和output的基础配置
1:entry和output就是打包的入口和出口的两个对象.但是如果入口文件就一个的话(应该说只希望打包出一个脚本文件), entry直接跟一个字符串(入口文件路径)就可以了.如:entry : &q ...
- Python--day46--mysql触发器
触发器:当对某张表做:增删改操作时,可以使用触发器自定义关联行为 1,为什么需要创建mysql触发器? 比如说我往tb1表里面插入一条数据的时候,同时需要往日志表tb2中插入这条数据,这时候就需要创造 ...
- Python--day39--管道和数据共享(面试可能会问到)
1,管道 上面所述挂起即为阻塞 管道.py from multiprocessing import Pipe, Process def func(conn1,conn2): conn2.close() ...
- Python--day60--建立第一个Djiango项目
- JVM堆内存管理与自定义分配参数详解
堆内存模型: 在Java中,堆被划分成两个不同的区域:新生代(Young),老年代(Old).而Permanent属于永久代(方法区),不属于堆内存.新生代又被分为了三个区域:Eden,from s ...
- python进阶之异常处理
异常处理 在代码运行时,会因为各种原因出现bug,而程序遇到bug就会中断运行,而在日常生产中程序是要长时间运行不能随意中断的.因此就需要我们提前做好异常处理. 异常 print(x) # 一般报错就 ...
- linux kdb 内核调试器
许多读者可能奇怪为什么内核没有建立更多高级的调试特性在里面.答案, 非常简单, 是 Linus 不相信交互式的调试器. 他担心它们会导致不好的修改, 这些修改给问题打了补丁 而不是找到问题的真正原因. ...
- vue-router再学习
vue路由: 1:动态路由配置 import Vue from 'vue' import Router from 'vue-router' import Index from '@/view/inde ...