一、BeanPostProcessor这个是spring容器的拓展之一,是用于获取bean的时候处理对应的对象;

  二、常用场景,在获取bean的时候,重新初始化bean的属性等。

  三、实现方式(加入容器后,调用其他bean的时候,通过BeanPostProcessor来进行处理)

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class BeanPostProcessorTest implements BeanPostProcessor{ /**
* 在bean加载之前处理
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("before");
return bean;
} /**
* 在bean加载之后处理
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("after");
return bean;
}
}

  四、源码部分

  1)容器中的注册部分为refresh()中的registerBeanPostProcessors()方法,注意这里只是注册,实际调用在getBean的时候

this.registerBeanPostProcessors(beanFactory);

  2)registerBeanPostProcessors

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//在容器中获取获取实现BeanPostProcessor的bean
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new AbstractApplicationContext.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
//声明不同BeanPostProcessor类型(priorityOrdered,internal,ordered,non 按照顺序,和优先级执行)
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList();
List<BeanPostProcessor> internalPostProcessors = new ArrayList();
List<String> orderedPostProcessorNames = new ArrayList();
List<String> nonOrderedPostProcessorNames = new ArrayList();
String[] var11 = postProcessorNames;
int var10 = postProcessorNames.length; BeanPostProcessor pp;
for(int var9 = 0; var9 < var10; ++var9) {
String ppName = var11[var9];
//判断时什么类型
if (this.isTypeMatch(ppName, PriorityOrdered.class)) {
pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
//此接口存在postProcessMergedBeanDefinition的接口
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
} else if (this.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
} else {
nonOrderedPostProcessorNames.add(ppName);
}
}
//排序
OrderComparator.sort(priorityOrderedPostProcessors);
//添加到beanPostProcessors(private final List<BeanPostProcessor> beanPostProcessors = new ArrayList();)
this.registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
List<BeanPostProcessor> orderedPostProcessors = new ArrayList();
Iterator var16 = orderedPostProcessorNames.iterator(); while(var16.hasNext()) {
String ppName = (String)var16.next();
BeanPostProcessor pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
} //排序
OrderComparator.sort(orderedPostProcessors);
this.registerBeanPostProcessors(beanFactory, orderedPostProcessors);
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList();
Iterator var19 = nonOrderedPostProcessorNames.iterator(); while(var19.hasNext()) {
String ppName = (String)var19.next();
pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
} this.registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
OrderComparator.sort(internalPostProcessors);
this.registerBeanPostProcessors(beanFactory, internalPostProcessors);
beanFactory.addBeanPostProcessor(new AbstractApplicationContext.ApplicationListenerDetector((AbstractApplicationContext.ApplicationListenerDetector)null));
} private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {
Iterator var4 = postProcessors.iterator(); while(var4.hasNext()) {
BeanPostProcessor postProcessor = (BeanPostProcessor)var4.next();
//添加到beanPostProcessors(private final List<BeanPostProcessor> beanPostProcessors = new ArrayList();)
beanFactory.addBeanPostProcessor(postProcessor);
}
}

  3)addBeanPostProcessor(DefaultListableBeanFactory,中缓存的List<String> beanDefinitionNames = new ArrayList())

  public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
this.beanPostProcessors.remove(beanPostProcessor);
//添加到beanPostProcessors中
this.beanPostProcessors.add(beanPostProcessor);
if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
this.hasInstantiationAwareBeanPostProcessors = true;
} if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors = true;
}
}

  五、调用方式(在个getBean的时候回自动按照优先级调用BeanPostProcessor)

  具体实现见:spring源码-bean之加载-2的四、源码实现3).d createBean部分

spring源码-BeanPostProcessor-3.3的更多相关文章

  1. 2.spring源码-BeanPostProcessor后置处理之ApplicationContextAwareProcessor,实现spring容器中某一个类的bean对象在初始化时需要得到Spring容器内容。

    需求:我们的需求是,在spring初始化完毕时,使我们自定义一个类Bird类可以得到spring容器内容. 实现步骤: 1.首先我们来看一下ApplicationContextAwareProcess ...

  2. 1.spring源码-BeanPostProcessor后置处理器

    1.BeanPostProcessor接口的介绍: BeanPostProcessor是一个接口,其中有两个方法,postProcessBeforeInitialization和postProcess ...

  3. [spring源码学习]五-BeanPostProcessor的使用

    一.接口描述 spring提供了一个接口类-BeanPostProcessor,我们叫他:bean的加工器,应该是在bean的实例化过程中对bean做一些包装处理,里边提供两个方法 public in ...

  4. Ioc容器BeanPostProcessor-Spring 源码系列(3)

    Ioc容器BeanPostProcessor-Spring 源码系列(3) 目录: Ioc容器beanDefinition-Spring 源码(1) Ioc容器依赖注入-Spring 源码(2) Io ...

  5. spring源码分析系列 (2) spring拓展接口BeanPostProcessor

    Spring更多分析--spring源码分析系列 主要分析内容: 一.BeanPostProcessor简述与demo示例 二.BeanPostProcessor源码分析:注册时机和触发点 (源码基于 ...

  6. Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制

    上一篇文章https://www.cnblogs.com/redwinter/p/16198942.html介绍了Spring的注解的解析过程以及Spring Boot自动装配的原理,大概回顾下:Sp ...

  7. spring源码:学习线索(li)

    一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...

  8. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

  9. spring源码分析(一)IoC、DI

    创建日期:2016.08.06 修改日期:2016.08.07 - 2016.08.12 交流QQ:992591601 参考书籍:<spring源码深度解析>.<spring技术内幕 ...

  10. Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)

    我们到底能走多远系列(33) 扯淡: 各位:    命运就算颠沛流离   命运就算曲折离奇   命运就算恐吓着你做人没趣味   别流泪 心酸 更不应舍弃   ... 主题: Spring源码阅读还在继 ...

随机推荐

  1. 关于SQL注入中编码问题的疑问

    提到SQL注入的绕过,编码是其中最普通的一种方法,最常用的URL编码.之前一直有个疑问,编码与未编码到底有哪些地方存在区别? 以下是本人自己对URL编码的一些见解,可能有错误的地方欢迎大佬们指正. 什 ...

  2. centos 在安装YouCompleteMe时提示 Fatal : pyconfig.h No such file or directory

    问题:centos 在安装YouCompleteMe时提示 Fatal : pyconfig.h No such file or directory 解决:安装python-devel yum ins ...

  3. webstorm window找不到文件'chrome'

    1.打开webstorm设置: File->Settings->Tools->Web Browsers->更改谷歌浏览器的Path(获取方式:谷歌浏览器的快捷键->右键- ...

  4. Mysql 基本语句 + 高级查询

    MySQL执行SQL脚本文件的命令: 从cmd进入mysql命令行模式: mysql> -uroot –prootpassword –Ddatabasename 如果是我本地的数据库,就相应修改 ...

  5. 关于content-type请求头的说明

    Content-Type请求头的作用,用于标记请求体数据的格式,如: 1. Content-Type:application/x-www-form-urlencoded 请求体:b'pwd=123&a ...

  6. python统计文档中词频

    python统计文档中词频的小程序 python版本2.7 效果如下: 程序如下,测试文件与完整程序在我的github中 #统计空格数与单词数 本函数只返回了空格数 需要的可以自己返回多个值 def ...

  7. 『ACM C++』 PTA 天梯赛练习集L1 | 052-053

    今日刷题,水题水题 ------------------------------------------------L1-052------------------------------------ ...

  8. js函数和window对象

  9. es6 Proxy对象详解

    Proxy用于修改某些操作的默认行为,也可以理解为在目标对象之前架设一层拦截,外部所有的访问都必须先通过这层拦截,因此提供了一种机制,可以对外部的访问进行过滤和修改.这个词的原理为代理,在这里可以表示 ...

  10. tcp总结与简单实现

    一.TCP简介 1. TCP介绍 1)TCP协议,传输控制协议(Transmission Control Protocol,缩写为 TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议 2)t ...