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

可见, 它是多么基础 而重要的一个 接口啊! 它提供了两个方法:
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的更多相关文章
- Spring 的 BeanPostProcessor接口实现
今天学习了一下Spring的BeanPostProcessor接口,该接口作用是:如果我们需要在Spring容器完成Bean的实例化,配置和其他的初始化后添加一些自己的逻辑处理,我们就可以定义一个或者 ...
- Spring中BeanPostProcessor
Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...
- spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(01)
在spring中beanPostProcessor绝对是开天辟地的产物,给了程序员很多自主权,beanPostProcessor即常说的bean后置处理器. 一.概览 先来说下Instantiatio ...
- spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)
在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...
- spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)
在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...
- 关于Spring的BeanPostProcessor
BeanPostProcessor接口作用是:如果我们需要在Spring容器完成Bean的实例化.配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcess ...
- Spring之BeanPostProcessor(后置处理器)介绍
为了弄清楚Spring框架,我们需要分别弄清楚相关核心接口的作用,本文来介绍下BeanPostProcessor接口 BeanPostProcessor 该接口我们也叫后置处理器,作用是在Be ...
- Spring 注册BeanPostProcessor 源码阅读
回顾上一篇博客中,在AbstractApplicationContext这个抽象类中,Spring使用invokeBeanFactoryPostProcessors(beanFactory);执行Be ...
- 【框架】利用Spring的BeanPostProcessor来修改bean属性
一.BeanPostProcessor是什么?什么时候触发?可以用来做什么? 1.它是什么? 首先它是一个接口,定义了两个方法: public interface BeanPostProcessor ...
- Spring的BeanPostProcessor和BeanFactoryPostProcessor区别
Spring提供了两种后处理bean的扩展接口,分别为BeanPostProcessor和BeanFactoryPostProcessor,这两者在使用上是有所区别的. BeanPostProcess ...
随机推荐
- man 命令帮助文件输出乱码
man 命令重定向的时候有写控制字符再直接显示的时候没有问题, 但是重定向到文件中的时候,被解释错误,显示为 ^H 或者乱码 解决方法: man ps | col -b >a.txt 可以消除所 ...
- poi读取word的内容
pache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1.读取word 2003及word 2007需要的 ...
- .NET并行计算和并发5:多线程编程一般指导性原则
使用多线程时要考虑以下准则: 不要使用 Thread.Abort 终止其他线程. 对另一个线程调用 Abort 无异于引发该线程的异常,也不知道该线程已处理到哪个位置. 不要使用 Thread.Sus ...
- setfacl语法
1.setfacl的用途setfacl命令可以用来细分linux下的文件权限. chmod命令可以把文件权限分为u,g,o三个组,而setfacl可以对每一个文件或目录设置更精确的文件权限. 换句话说 ...
- Linux下初次使用github
1.安装 1.1 使用yum安装的 命令:$ yum install git git-gui 1.2 生成密钥对,使用ssh-keygen方法 ssh-keygen -t [rsa|dsa],将会生成 ...
- WEBBASE篇: 第七篇, JavaScript知识1
JavaScript 1 一.JavaScript 概述什么是JavaScript: JavaScript 简称 JS,是一种专门运行于JS解释器/引擎中的解释型脚本语言JS发展史: 1.1992年N ...
- Windows下运行Linux命令
安装Gow软件,Gow-0.7.0.exe,这样就可以在Windows命令行运行Linux命令,比如通过scp把Windows下的文件拷贝到Linux下. 直接运行安装,不会生成任何客户端,直接使用W ...
- day04列表
列表 内容详细 1.列表 公共 独有方法 删除 remove pop clear del区别 强制转换 #表示多个事物 users=["lili","Joe", ...
- node day2 vue read html
app.js var http = require("http"); var fs = require('fs'); var url = require('url'); http. ...
- python写入txt文件时的覆盖和追加
python写入文件时的覆盖和追加 在使用Python进行txt文件的读写时,当打开文件后,首先用read()对文件的内容读取,然后再用write()写入,这时发现虽然是用"r+" ...