作用和调用时机

spring有两种后置处理器:

1. 组件后置处理器——org.springframework.beans.factory.config.BeanPostProcessor;

2. 工厂后置处理器——org.springframework.beans.factory.config.BeanFactoryPostProcessor。

根据名字可以看出,前者是处理组件的,后者是处理组件工厂的。

BeanPostProcessor的调用时机是在bean被实例化之后、初始化之前或之后的;

那么,BeanFactoryPostProcessor的调用时机是什么呢?先来截取一段sring源码。

@FunctionalInterface
public interface BeanFactoryPostProcessor { /**
* Modify the application context's internal bean factory after its standard
* initialization. All bean definitions will have been loaded, but no beans
* will have been instantiated yet. This allows for overriding or adding
* properties even to eager-initializing beans.
* @param beanFactory the bean factory used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }

从源码可以看出,BeanFactoryPostProcessor是一个接口,只有1个方法,用于修改springIOC容器内部的bean工厂,调用时机是容器标准初始化之后。什么是标准初始化?注释后面有解释:所有的bean定义都即将被加载,但是还没有bean被实例化。所以,BeanFactoryPostProcessor的方法肯定是在BeanPostProcessor的两个方法之前被调用的。

示例Demo

自定义BeanFactoryPostProcessor实现类

/**
* 自定义的BeanFactoryPostProcessor,即工厂后置处理器
* created on 2019-04-21
*/
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
/**
* 这个方法来自于BeanFactoryPostProcessor接口
* 用于修改关于容器的配置
* 调用时机:容器的标准初始化之后,这时所有的bean定义将要加载、但是并没有bean被实例化
* @param beanFactory
* @throws BeansException
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int count = beanFactory.getBeanDefinitionCount();
String[] names = beanFactory.getBeanDefinitionNames();
System.out.println("MyBeanFactoryPostProcessor.postProcessBeanFactory正在执行");
System.out.println("共有" + count + "个组件,它们是:" + Arrays.asList(names));
}
}

普通bean类

/**
* bean类,通过实现接口的方式获取初始化和销毁时调用的方法
*/
@Component
public class Dog implements InitializingBean, DisposableBean { //属性
private String name;
private int age; //构造方法
public Dog() {
System.out.println("Dog:正在调用无参构造方法");
}
public Dog(String name, int age) {
System.out.println("正在调用带参构造方法");
this.name = name;
this.age = age;
} @Override
public void destroy() throws Exception {
System.out.println("Dog:正在调用销毁方法");
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("Dog:正在调用初始化方法");
} //getters & setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

容器配置类,自定义的工厂后置处理器必须在容器中注册才能生效

/**
* 容器配置类
* 注册自定义BeanFactoryPostProcessor
*/
@Configuration
@Import(value = {MyBeanFactoryPostProcessor.class})
public class BeanFactoryPostProcessorConfig { //注册普通bean类
@Bean
@Scope("singleton")
public Dog dog() {
return new Dog();
}
}

测试类

/**
* 用于测试自定义BeanFactoryPostProcessor
*/
public class BeanFactoryPostProcessorTest { @Test
public void test() {
//创建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanFactoryPostProcessorConfig.class);
//关闭容器
applicationContext.registerShutdownHook();
}
}

测试结果

MyBeanFactoryPostProcessor.postProcessBeanFactory正在执行
共有9个组件,它们是:[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalCommonAnnotationProcessor,
org.springframework.context.event.internalEventListenerProcessor,
org.springframework.context.event.internalEventListenerFactory,
beanFactoryPostProcessorConfig, cn.monolog.processors.MyBeanFactoryPostProcessor,
cn.monolog.processors.MyBeanDefinitionRegistryPostProcessor,
dog]
Dog:正在调用无参构造方法
Dog:正在调用初始化方法
Dog:正在调用销毁方法

从测试结果可以看出:

BeanFactoryPostProcessor.postProcessBeanFactory方法被调用时,容器中确实已经有关于bean的定义,但是bean的实例化却是后来才执行的。

源码分析

我们从创建springIOC容器开始


org.springframework.context.annotation.AnnotationConfigApplicationContext
/**
* Create a new AnnotationConfigApplicationContext, deriving bean definitions
* from the given annotated classes and automatically refreshing the context.
* @param annotatedClasses one or more annotated classes,
* e.g. {@link Configuration @Configuration} classes
*/
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
   this();
register(annotatedClasses);
refresh();
}

跟进refresh →


org.springframework.context.support.AbstractApplicationContext
    @Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory); try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); //1 // Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
initMessageSource(); // Initialize event multicaster for this context.
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
onRefresh(); // Check for listener beans and register them.
registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory); //2 // Last step: publish corresponding event.
finishRefresh();
} catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
} // Destroy already created singletons to avoid dangling resources.
destroyBeans(); // Reset 'active' flag.
cancelRefresh(ex); // Propagate exception to caller.
throw ex;
} finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}

在1处调用的是组件工厂后置处理器的方法,在2处才是bean实例化,继续跟进invokeBeanFactoryPostProcessors →


org.springframework.context.support.AbstractApplicationContext
    /**
* Instantiate and invoke all registered BeanFactoryPostProcessor beans,
* respecting explicit order if given.
* <p>Must be called before singleton instantiation.
*/
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}

首先调用了一个静态方法,后面是一堆关于类加载器的操作,跟进这个静态方法 →

org.springframework.context.support.PostProcessorRegistrationDelegate
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { // Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>(); if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
} // Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear(); // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear(); // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
} // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
} else {
// Invoke factory processors registered with the context instance.
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
} // Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); //1
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
} // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); //2 // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); //3 // Finally, invoke all other BeanFactoryPostProcessors.
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); //4 // Clear cached merged bean definitions since the post-processors might have
// modified the original metadata, e.g. replacing placeholders in values...
beanFactory.clearMetadataCache();
}

这个静态方法很长,前面有一段关于beanFactory instanceof BeanDefinitionRegistry的分支,会优先执行。这个分支是用来处理BeanDefinitionRegistryPostProcessor的,它是BeanFactoryPostProcessor的一个子接口,用于向容器中添加新的bean定义,调用时机在别的BeanFactoryPostProcessor之前。我们先不看它,直接看下面的普通BeanFactoryPostProcessor——

1处:从容器中根据类型获取所有的BeanFactoryPostProcessor的bean名称;

2处:首先执行实现了PriorityOrdered接口的BeanFactoryPostProcessor;

3处:然后执行实现了Ordered接口的BeanFactoryPostProcessor;

4处:最后执行其他的BeanFactoryPostProcessor,即没有实现任何排序接口的。

spring的组件工厂后置处理器——BeanFactoryPostProcessor的更多相关文章

  1. spring 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别

    主要区别就是: BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能,下面举个例子说明 BEAN类: package com.spring ...

  2. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  3. Spring中Bean的后置处理器

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-post-processors.html: Bean后置处理器 BeanPostProce ...

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

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

  5. Spring的后置处理器BeanFactoryPostProcessor

    新建一个JavaBean UserBeanFactoryPostProcessor 实现了BeanFactoryPostProcessor接口 Spring配置文件如下: 编写测试用例 从结果可以看出 ...

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

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

  7. Spring点滴五:Spring中的后置处理器BeanPostProcessor讲解

    BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...

  8. Spring Bean前置后置处理器的使用

    Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下B ...

  9. spring学习四:Spring中的后置处理器BeanPostProcessor

    BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...

随机推荐

  1. gulp.js实现less批量实时编译

    问题描述: 在之前一直用Koala编译less文件,但本人感觉Koala用起来非常麻烦,好像不能做多个less文件的批量的编译:因为目前项目也没有用到webpack,我的less是通过vs code ...

  2. Linux练习例题(附答案)

    1.通过ps命令的两种选项形式查看进程信息 2.通过top命令查看进程 3.通过pgrep命令查看sshd服务的进程号 4.查看系统进程树 5.使dd if=/dev/zero of=/root/fi ...

  3. RBAC | YAML |

    YAML配置文件: 1.凡是可以在application.properties配置的文件,都可以在application.yaml文件中配置 2.properties的优先级大于yaml的优先级 后端 ...

  4. php set_magic_quotes_runtime() 函数过时解决方法

    PHP5.3中 bool set_magic_quotes_runtime ( bool $new_setting )函数过时.把函数: set_magic_quotes_runtime($newse ...

  5. ui自动化之selenium操作(四)简单元素操作

    1. clear() clear()方法用于清除文本输入框内的内容:一般输入框中都有默认文字,如果不清空有可能会导致字符拼接: browser.find_element(By.ID,"use ...

  6. jmeter之jtl文件解析(生成测试报告)命令行

    jmeter -g TestReport201905060302.jtl -o ./report 1:命令行模式将jtl转成测试图表-注意此方法只使用jmeter3.0以后版本 第一种:在测试过程中将 ...

  7. 一图解决五大I/O模型比较

  8. react项目导出数据怎么做?

    做项目遇到导出数据,搜索了一个插件,简直太好用,几行代码就可以搞定. 插件是react-csv, 了解详细介绍大家可以去https://www.npmjs.com/package/react-csv

  9. msyql 优化之五不要

    1.尽量不要有空判断的语句,因为空判断将导致全表扫描,而不是索引扫描. 对于空判断这种情况,可以考虑对这个列创建数据库默认值 //空判断将导致全表扫描 select small_id from sma ...

  10. 与HTTP协作的Web服务器——代理、网关、隧道

    1.虚拟主机 (1)HTTP/1.1规范允许一台HTTP服务器搭建多个Web站点: (2)在互联网上,域名通过DNS服务映射到IP地址(域名解析)之后访问目标网站,即当请求发送到服务器时,已经是以IP ...