粗略一看, 它有这么多实现:

可见, 它是多么基础 而重要的一个 接口啊! 它提供了两个方法:

public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object var1, String var2) throws BeansException; Object postProcessAfterInitialization(Object var1, String var2) throws BeansException;
}

两个方法的名字 都是  postProcessXxxInitialization 的格式, 不是 Before 就是 After,  都是围绕 process Initialization  这个过程来做文章的。  当然, 我有一点不明白的是, 为什么  这里有个post, post作为前缀有 在... 之后的意思(http://skill.qsbdc.com/cigencizhui/328.html),但是在这里,和Before一起使用, 有些难以理解。

如果我们自定义的class 实现了 这个接口,  那么它会在每一个 bean 的实例化前后 各被调用一次。 参见AbstractAutowireCapableBeanFactory 源码:

    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Iterator var4 = this.getBeanPostProcessors().iterator(); do {
if(!var4.hasNext()) {
return result;
} BeanPostProcessor beanProcessor = (BeanPostProcessor)var4.next();
result = beanProcessor.postProcessBeforeInitialization(result, beanName); // 前处理
} while(result != null); return result;
} public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Iterator var4 = this.getBeanPostProcessors().iterator(); do {
if(!var4.hasNext()) {
return result;
} BeanPostProcessor beanProcessor = (BeanPostProcessor)var4.next();
result = beanProcessor.postProcessAfterInitialization(result, beanName);// 后处理
} while(result != null); return result;
}

这两个方法都是在初始化bean 的时候被调用的:

    protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if(System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean);
return null;
}
}, this.getAccessControlContext());
} else {
this.invokeAwareMethods(beanName, bean);
} Object wrappedBean = bean;
if(mbd == null || !mbd.isSynthetic()) {
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName); // 前处理
} try {
this.invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable var6) {
throw new BeanCreationException(mbd != null?mbd.getResourceDescription():null, beanName, "Invocation of init method failed", var6);
} if(mbd == null || !mbd.isSynthetic()) {
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);// 后处理
} return wrappedBean;
}

spring 之 BeanPostProcessor的更多相关文章

  1. Spring 的 BeanPostProcessor接口实现

    今天学习了一下Spring的BeanPostProcessor接口,该接口作用是:如果我们需要在Spring容器完成Bean的实例化,配置和其他的初始化后添加一些自己的逻辑处理,我们就可以定义一个或者 ...

  2. Spring中BeanPostProcessor

    Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...

  3. spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(01)

    在spring中beanPostProcessor绝对是开天辟地的产物,给了程序员很多自主权,beanPostProcessor即常说的bean后置处理器. 一.概览 先来说下Instantiatio ...

  4. spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...

  5. spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...

  6. 关于Spring的BeanPostProcessor

    BeanPostProcessor接口作用是:如果我们需要在Spring容器完成Bean的实例化.配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcess ...

  7. Spring之BeanPostProcessor(后置处理器)介绍

      为了弄清楚Spring框架,我们需要分别弄清楚相关核心接口的作用,本文来介绍下BeanPostProcessor接口 BeanPostProcessor   该接口我们也叫后置处理器,作用是在Be ...

  8. Spring 注册BeanPostProcessor 源码阅读

    回顾上一篇博客中,在AbstractApplicationContext这个抽象类中,Spring使用invokeBeanFactoryPostProcessors(beanFactory);执行Be ...

  9. 【框架】利用Spring的BeanPostProcessor来修改bean属性

    一.BeanPostProcessor是什么?什么时候触发?可以用来做什么? 1.它是什么? 首先它是一个接口,定义了两个方法: public interface BeanPostProcessor ...

  10. Spring的BeanPostProcessor和BeanFactoryPostProcessor区别

    Spring提供了两种后处理bean的扩展接口,分别为BeanPostProcessor和BeanFactoryPostProcessor,这两者在使用上是有所区别的. BeanPostProcess ...

随机推荐

  1. Full permutation

    Full Permutation 全排列问题, 将1~n这n个整数按字典序排放 划分: 输出1开头的全排列 输出2开头的全排列 ...... 输出n开头的全排列 递归边界:当下标1 ~ n 位都已经填 ...

  2. VC++、MFC Sqlite3数据库的使用

    SQLite数据库是一种本地的轻型数据库,在存储一些本地的数据的时候,或者不需要用到Oracle,SQL2008之类的大型数据库的时候,Sqlite的优势就能够得到发挥.程序需要采集数据存储起来,可以 ...

  3. GoogLeNet 之 Inception-v1 解读

    本篇博客的目的是展示 GoogLeNet 的 Inception-v1 中的结构,顺便温习里面涉及的思想. Going Deeper with Convolutions:http://arxiv.or ...

  4. 几个特殊的IP地址

    1)私有地址     IP地址在全世界范围内唯一,看到这句话你可能有这样的疑问,像192.168.0.1这样的地址在许多地方都能看到,并不唯一,这是为何?Internet管理委员会规定如下地址段为私有 ...

  5. SQLMap工具的安装使用

    SQLMap工具介绍: sqlmap是一个开源软件,用于检测和利用数据库漏洞,并提供将恶意代码注入其中的选项. 它是一种渗透测试工具,可自动检测和利用SQL注入漏洞,在终端中提供其用户界面.该软件在命 ...

  6. c# ef

    找出不同项 ).ToList(); resultMsg = string.Join(",", query.select(p=>p.key).ToList())

  7. 第2章 Java基本语法(上): 变量与运算符

    2-1 关键字与保留字 关键字(keyword) 保留字(reserved word) 2-2 标识符(Identifier) 案例 class Test{ public static void ma ...

  8. phpStudy2018 在win7下切换php7不成功解决办法

    phpstudy 由2016升级到2018后,在切换版本时,php5.6及以下版本可以正常切换,切换7.0以上的版本时访问页面报 0xc000007b 错误,网上找了很多方法都没能解决,最后发现是没装 ...

  9. map/reduce/filter/lambda

    Python内建了map()/reduce()/filter()函数. map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的It ...

  10. mysql 远程备份

    #远程备份./innobackupex --defaults-file=/etc/my.cnf --no-timestamp --user xxx --host 192.168.1.123 \--pa ...