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 ...
随机推荐
- 使用json改写网站
效果图 json文件 https://raw.githubusercontent.com/sherryloslrs/timetable/master/timetable.json { "Ti ...
- DoTween的用法
using UnityEngine;using System.Collections;using DG.Tweening;using UnityEngine.UI; public class Test ...
- union 和 case
UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果. CASE更多的是一种判断分类,就想其他语言的case一样 1. CASE WHEN 表达式有两种形式 --简单Ca ...
- vue-cli 安装过程出现错误
如果是这样得错误,那是你在安装sass得问题,需要安装python2,安装好就行了
- python基础07_tuple_dict
tuple 元组 dict 字典 更详细参考:https://www.cnblogs.com/jin-xin/articles/7562422.html #!/usr/bin/env pyth ...
- TestLink测试管理工具的使用举例—第二篇
本篇博客接上面TestLink测试管理工具的使用举例—第一篇的内容继续讲解如何使用TestLink工具进行测试管理. 创建一个名为“购物V1.1系统测试”的测试计划. 2.2版本管理 点击主页“测试计 ...
- 剑指Offer 16. 合并两个排序的链表 (链表)
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 题目地址 https://www.nowcoder.com/practice/d8b6b4358 ...
- Ubuntu 17.10 安装Caffe(cpu)并配置Matlab接口
(1)安装依赖: sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-ser ...
- jQuery-3.事件篇---表单事件
jQuery表单事件之blur与focus事件 在之前2.8与2.9节我们学过了表单处理事件focusin事件与focusout事件,同样用于处理表单焦点的事件还有blur与focus事件 它们之间的 ...
- Redis占硬盘空间
转载自:http://blog.csdn.net/qq285744011/article/details/51002409 [问题的原因] Windows版Redis启动后,会在C盘自动创建一个很大的 ...