/**
*  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);
}
}
}
  1. 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;
}
  1. 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);
}
}
}
  1. 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的更多相关文章

  1. 【Spring注解驱动开发】BeanPostProcessor在Spring底层是如何使用的?看完这篇我懂了!!

    写在前面 在<[String注解驱动开发]面试官再问你BeanPostProcessor的执行流程,就把这篇文章甩给他!>一文中,我们详细的介绍了BeanPostProcessor的执行流 ...

  2. 【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?

    写在前面 在<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!>一文中,我们讲述了如何使用@Bean注解来指定bean初始化和销毁的方法.具体的 ...

  3. 【Spring注解驱动开发】关于BeanPostProcessor后置处理器,你了解多少?

    写在前面 有些小伙伴问我,学习Spring是不是不用学习到这么细节的程度啊?感觉这些细节的部分在实际工作中使用不到啊,我到底需不需要学习到这么细节的程度呢?我的答案是:有必要学习到这么细节的程度,而且 ...

  4. 学会使用Spring注解

      概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 ...

  5. Spring注解详解

    概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...

  6. spring注解和xml方式区别详解

    一.spring常规方式. 在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立.下面是 3 个类,它们分别是 Office.Car 和 Boss,这 3 ...

  7. 【转】Spring注解详解

    http://blog.csdn.net/xyh820/article/details/7303330/ 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类 ...

  8. spring注解驱动开发

    1.全图: 一.IOC容器部分 1.第一个初始化实例: @Configuration @ComponentScans @Bean("person") 注意: @repeatable ...

  9. 看Spring注解之IOC记录

    首先看源码里有些是java的元注解记录的有如下几个: @Inherited注释:指明被注解的类会自动继承.更具体地说,如果定义注解时使用了 @Inherited 标记,然后用定义的注解来标注另一个父类 ...

随机推荐

  1. getAttribute与getParameter的区别

    1.getParameter得到的是字符串,其取值源于jsp页面,从jsp页面中接受一个存在的参数,多用于servlet中,用于判断业务的类型和跳转页面.如: request.getParameter ...

  2. Java 静态方法不能重写但可以被子类静态方法覆盖

    强调 静态方法是属于类的,只存在一份,会被该类的所有对象共享.不可以被重写. 静态方法可以被子类继承,但是不可以被子类重写 class door{ } class wood_Door extends ...

  3. 【译】第42节---EF6-DbSet.AddRange & DbSet.RemoveRange

    原文:http://www.entityframeworktutorial.net/entityframework6/addrange-removerange.aspx EF 6中的DbSet引入了新 ...

  4. Win10远程桌面可能是由于CredSSP加密Oracle修正

    win10更新1083之后,远程桌面就会连接失败,显示如下: 根据微软官方的说法是更改了安全策略: https://support.microsoft.com/zh-cn/help/4093492/c ...

  5. 1 --- Vue 基础指令

    1.vue 指令 1.v-model  主要在表单中使用,文本框.teaxare.单选.下拉 等 2.v-text   文本渲染  类似{{}} 3.v-show  控制Dom显示隐藏   displ ...

  6. CentOS6.5下搭建LAMP+FreeRadius+Daloradius Web管理和TP-LINK路由器、H3C交换机连接,实现,上网认证和记账功能

    什么是RADIUS服务: RADIUS:(Remote Authentication Dial In User Service)中文名为远程用户拨号认证服务,简称RADIUS,是目前应用最广泛的AAA ...

  7. npm i和npm install的区别

    最近人用npm i来直接安装模块,但是有会报错,用npm install就不会报错,刚开始百思不得其解,它俩明明是同一个东西 后来查npm的帮助指令发现还是没区别,npm i仅仅是npm instal ...

  8. 学习笔记11—MATLAB 界面设计

    1.cmd窗口输入-guide------> 打开.fig文件 2.查看SPM源代码: 2.matlab中如何改x,y轴以及图例上字体大小 1) x,y轴 -------整个轴上面就一个设定字符 ...

  9. kbengine学习2 创建项目

    官方文档https://www.comblockengine.com/docs/1.0/get-started/createproject/ 1.kbe服务器端 1.1 复制出一个assets文件夹, ...

  10. ubuntu 中文设置

    1,安装中文语言包 sudo apt-get install language-pack-zh-hans sudo update-locale LANG=zh_CN.UTF-8 添加中文支持: sud ...