Spring注解之BeanPostProcessor与InitializingBean
/**
* BeanPostProcessor 为每个bean实例化时提供个性化的修改,做些包装等
*/
package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; public interface BeanPostProcessor { /**
* 在bean实例化前调用*/
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; /**
* 在bean实例化后调用,如果bean实现了InitializingBean,则在执行完* 该接口的afterPropertiesSet方法后调用 ,如果实现了init-method则 * 在执行完init-method后调用*/
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }
package org.springframework.beans.factory; /**
/* *InitializingBean 为实现该接口的bean提供默认的初始化方法 *也可以在xml配置bean的使用init-method来实现初始化方法 */*/
public interface InitializingBean { void afterPropertiesSet() throws Exception; }
InitializingBean和BeanPostProcessor的执行顺序:构造方法-->BeanPostProcessor-->InitializingBean-->bean中的初始化方法
bean的最终初始化是由AbstractAutowireCapableBeanFactory的initializeBean方法来完成的,下面是该方法的源码
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
**invokeAwareMethods**(beanName, bean);
return null;
}
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = **applyBeanPostProcessorsBeforeInitialization**(wrappedBean, beanName);
}
try {
**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()) {
wrappedBean = **applyBeanPostProcessorsAfterInitialization**(wrappedBean, beanName);
}
return wrappedBean;
}
可以看到initializeBean主要有四步骤
1. invokeAwareMethods 调用实现Aware接口方法,这里只针对三种Aware接口BeanNameAware,BeanClassLoaderAware,BeanFactoryAware,方法如下
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
- applyBeanPostProcessorsBeforeInitialization 调用所有实现BeanPostProcessor类 的 postProcessBeforeInitialization方法,源码如下
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException { Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessBeforeInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
- invokeInitMethods 调用bean的初始化方法,如果bean实现了InitializingBean 接口,则调用afterPropertiesSet方法,如果有配置init-method,则调用配置的初始化方法,源码如下:
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
((InitializingBean) bean).afterPropertiesSet();
return null;
}
}, getAccessControlContext());
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
((InitializingBean) bean).**afterPropertiesSet**();
}
} if (mbd != null) {
String initMethodName = mbd.getInitMethodName();
if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
**invokeCustomInitMethod**(beanName, bean, mbd);
}
}
}
- applyBeanPostProcessorsAfterInitialization 调用所有实现BeanPostProcessor类 的 postProcessAfterInitialization方法,源码如下
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException { Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessAfterInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
注:https://blog.csdn.net/zhaiyang1992/article/details/52200602
Spring注解之BeanPostProcessor与InitializingBean的更多相关文章
- 【Spring注解驱动开发】BeanPostProcessor在Spring底层是如何使用的?看完这篇我懂了!!
写在前面 在<[String注解驱动开发]面试官再问你BeanPostProcessor的执行流程,就把这篇文章甩给他!>一文中,我们详细的介绍了BeanPostProcessor的执行流 ...
- 【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?
写在前面 在<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!>一文中,我们讲述了如何使用@Bean注解来指定bean初始化和销毁的方法.具体的 ...
- 【Spring注解驱动开发】关于BeanPostProcessor后置处理器,你了解多少?
写在前面 有些小伙伴问我,学习Spring是不是不用学习到这么细节的程度啊?感觉这些细节的部分在实际工作中使用不到啊,我到底需不需要学习到这么细节的程度呢?我的答案是:有必要学习到这么细节的程度,而且 ...
- 学会使用Spring注解
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 ...
- Spring注解详解
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- spring注解和xml方式区别详解
一.spring常规方式. 在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立.下面是 3 个类,它们分别是 Office.Car 和 Boss,这 3 ...
- 【转】Spring注解详解
http://blog.csdn.net/xyh820/article/details/7303330/ 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类 ...
- spring注解驱动开发
1.全图: 一.IOC容器部分 1.第一个初始化实例: @Configuration @ComponentScans @Bean("person") 注意: @repeatable ...
- 看Spring注解之IOC记录
首先看源码里有些是java的元注解记录的有如下几个: @Inherited注释:指明被注解的类会自动继承.更具体地说,如果定义注解时使用了 @Inherited 标记,然后用定义的注解来标注另一个父类 ...
随机推荐
- log4j2打印Mybatis执行的SQL语句及SQL语句的执行时间
http://blog.csdn.net/zjq852533445/article/details/78320012
- js操作css变量
原文:http://css-live.ru/articles/dostup-k-css-peremennym-i-ix-izmenenie-spomoshhyu-javascript.html :ro ...
- codeforces 15C. Industrial Nim
题目链接:http://codeforces.com/problemset/problem/15/C $NIM$游戏是次要的,直接异或石头堆就可以了,问题在于给出的石头堆的数量极多. 考虑利用异或的性 ...
- [osg]节点遍历nodevisitor浅析
参考:https://www.cnblogs.com/hzhg/archive/2010/12/17/1908764.html OSG中节点的访问使用的是一种访问器模式.一个典型的访问器涉及抽象访问者 ...
- Eclipse使用之将Git项目转为Maven项目, ( 注意: 最后没有pom.xml文件的, 要转化下 )
Eclipse使用之将Git项目转为Maven项目(全图解) 2017年08月11日 09:24:31 阅读数:427 1.打开Eclipse,File->Import 2.Git->Pr ...
- Corrupted Metadata/failed to mount /sysroot
公司电脑CentOS系统突然断电,开机后,无法进入系统.查看系统log如下: mount: mount /dev/mapper/cl-root on /sysroot failed:Structure ...
- 牛客OI周赛4-提高组 C 战争(war)
战争(war) 思路: 二分答案, 找到第一个不满足条件的位置 首先对于一个值来说, 所有这个值的区间肯定有交区间, 然后在这个交区间内不能出现比它小的数 所以我们check时从大的值开始考虑, 求出 ...
- Android+Servlet+MySql+JSON实现简单的数据查询操作--C/S架构
本例简单地实现Android客户端与服务器端交互,主要是通过客户端输入内容(学号)提交到服务器端,服务器端与数据库交互去查询相应信息(姓名).根据这个做个完整的安卓登录是没问题的.本例数据库服务器都采 ...
- WPF自定义漂亮的按钮样式
首先打开 Microsoft Visual Studio 2008 ,新建一个WPF项目,在上面随便放几个按钮: 然后给各个按钮设置不同的背景颜色: 设置好之后就是这样啦: 然后我们就开始在 App. ...
- LeetCode--350--两个数组的交集2
问题描述: 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = ...