BeanFactoryPostProcessor源码分析

BeanFactoryPostProcessor概述

Spring提供了BeanFactoryPostProcessor来对Spring的BeanFactory进行扩展。

Spring的BeanFactoryPostProcessor大致分为两大类:

(1)对BeanFactory中的Bean进行修改:org.springframework.beans.factory.config.BeanFactoryPostProcessor

(2)对BeanFactory进行新增Bean:org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor,此接口是BeanFactoryPostProcessor的子接口。

BeanFactoryPostProcessor

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可以在应用程序上下文的标准初始化之后修改它的内部bean工厂。所有bean定义都已加载,但还没有实例化bean。这允许覆盖或添加属性,甚至是对急于初始化的bean。

BeanFactoryPostProcessor(或它的子接口)就像一个黑客一样,入侵到Spring容器中,可以对Spring容器中的Bean进行增删改查等操作。

BeanFactoryPostProcessor演示

编写Bean

@Data
public class Person {

	private String name;
	
	private Integer age;
	
	private String sex;
	
}

编写BeanFactoryPostProcessor实现类

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        String[] persons = beanFactory.getBeanNamesForType(Person.class);
        String personBeanName = persons[0];
        BeanDefinition personBeanDefinition = 
            beanFactory.getBeanDefinition(personBeanName);
        MutablePropertyValues mutablePropertyValues = 
            personBeanDefinition.getPropertyValues();
        mutablePropertyValues.add("name", "lisi");
        mutablePropertyValues.add("age", "30");
    }

}

编写Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<bean class="com.spring.postprocessor.factory.MyBeanFactoryPostProcessor"/>

    <bean id="" class="com.spring.beans.Person">
		<property name="name" value="zhangsan"/>
		<property name="age" value="20"/>
		<property name="sex" value="man"/>
	</bean>
    
</beans>

测试代码

public static void main(String[] args) {

    ClassPathXmlApplicationContext context = 
        new ClassPathXmlApplicationContext("applicationContext.xml");

    Person person = context.getBean(Person.class);
	System.out.println(person);
    context.close();
}

输出结果:

Person(name=lisi, age=30, sex=man)

咦?我明明配置的是name=zhangsan,age=20,怎么变成了name=lisi, age=30呢?

看样子是在MyBeanFactoryPostProcessor#postProcessBeanFactory中被修改了。Spring执行步骤可以通过debug查看。

FactoryPostProcessor源码分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M2EqOLAt-1582393148730)(E:/java-knowledge/spring/images/BeanFactoryPostProcessor的Spring执行步骤图.png)]

通过对MyBeanFactoryPostProcessor#postProcessBeanFactory打断点,可以看到此方法在Spring中的执行步骤。直接定位到AbstractApplicationContext#invokeBeanFactoryPostProcessors方法中:

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    // invokeBeanFactoryPostProcessors此方法是执行MyBeanFactoryPostProcessor#postProcessBeanFactory的位置。代码逻辑很长,会单独一个代码块进行讲解
    // TODO
    // getBeanFactoryPostProcessors()是获取当前应用上下文中已经注册的 BeanFactoryPostProcessor,默认情况是为空
    PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(
        							beanFactory, getBeanFactoryPostProcessors());
  //...
}

PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory, List<BeanFactoryPostProcessor>):

public static void invokeBeanFactoryPostProcessors(
    ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

    // Invoke BeanDefinitionRegistryPostProcessors first, if any.
    Set<String> processedBeans = new HashSet<String>();

    if (beanFactory instanceof BeanDefinitionRegistry) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        // 用于存放BeanFactoryPostProcessor
        List<BeanFactoryPostProcessor> regularPostProcessors = 
            new LinkedList<BeanFactoryPostProcessor>();
        // 用于存放BeanDefinitionRegistryPostProcessor
        List<BeanDefinitionRegistryPostProcessor> registryProcessors = 
            new LinkedList<BeanDefinitionRegistryPostProcessor>();
		// 1、遍历beanFactoryPostProcessors(context中定义的,默认情况为空):将BeanDefinitionRegistryPostProcessor和普通BeanFactoryPostProcessor区分开
        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                //1.1如果是BeanDefinitionRegistryPostProcessor
                BeanDefinitionRegistryPostProcessor registryProcessor =
                    (BeanDefinitionRegistryPostProcessor) postProcessor;
                // 1.1.1 直接执行BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry方法
                registryProcessor.postProcessBeanDefinitionRegistry(registry);
                // 1.1.2 添加到registryProcessors(用于最后执行postProcessBeanFactory方法)
                registryProcessors.add(registryProcessor);
            }
            else {
                //1.2否则,只是普通的BeanFactoryPostProcessor,添加到regularPostProcessors(用于最后执行postProcessBeanFactory方法)
                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.
        //用于保存本次要执行的BeanDefinitionRegistryPostProcessor
        List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();

        // 2.调用所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor实现类
        // 2.1找出所有实现BeanDefinitionRegistryPostProcessor接口的Bean的beanName
        String[] postProcessorNames = beanFactory.getBeanNamesForType(
            BeanDefinitionRegistryPostProcessor.class, true, false);
        // 2.2 遍历postProcessorNames
        for (String ppName : postProcessorNames) {
            // // 2.3 校验是否实现了PriorityOrdered接口
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                // 2.4 获取ppName对应的bean实例, 添加到currentRegistryProcessors中
                currentRegistryProcessors.add(beanFactory.getBean(
                    ppName, BeanDefinitionRegistryPostProcessor.class));
                // 2.5 将要被执行的加入processedBeans,避免后续重复执行
                processedBeans.add(ppName);
            }
        }
        // 2.6 进行排序(根据是否实现PriorityOrdered、Ordered接口和order值来排序)
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        // 2.7 添加到registryProcessors(用于最后执行postProcessBeanFactory方法)
        registryProcessors.addAll(currentRegistryProcessors);
        // 2.8 遍历currentRegistryProcessors, 执行postProcessBeanDefinitionRegistry方法
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
        // 2.9 执行完毕后, 清空currentRegistryProcessors
        currentRegistryProcessors.clear();

        // 3.调用所有实现了Ordered接口的BeanDefinitionRegistryPostProcessor实现类(和步骤2类似)
        // 3.1 找出所有实现BeanDefinitionRegistryPostProcessor接口的类, 这边重复查找是因为执行完上面的BeanDefinitionRegistryPostProcessor,可能会新增了其他的BeanDefinitionRegistryPostProcessor, 因此需要重新查找
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        for (String ppName : postProcessorNames) {
            // 校验是否实现了Ordered接口,并且还未执行过
            if (!processedBeans.contains(ppName) && 
                beanFactory.isTypeMatch(ppName, Ordered.class)) {
                currentRegistryProcessors.add(beanFactory.getBean(
                    ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        //3.2 进行排序(根据是否实现PriorityOrdered、Ordered接口和order值来排序)
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        registryProcessors.addAll(currentRegistryProcessors);
        //3.3 遍历currentRegistryProcessors, 执行postProcessBeanDefinitionRegistry方法
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
        currentRegistryProcessors.clear();

        // 4.最后, 调用所有剩下的BeanDefinitionRegistryPostProcessors
        boolean reiterate = true;
        while (reiterate) {
            reiterate = false;
            // 4.1 找出所有实现BeanDefinitionRegistryPostProcessor接口的类
            postProcessorNames = beanFactory.getBeanNamesForType(
                BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                // 4.2 跳过已经执行过的
                if (!processedBeans.contains(ppName)) {
                    // 4.3 如果有BeanDefinitionRegistryPostProcessor被执行, 则有可能会产生新的BeanDefinitionRegistryPostProcessor。因此这边将reiterate赋值为true, 代表需要再循环查找一次
                    currentRegistryProcessors.add(beanFactory.getBean(
                        ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                    reiterate = true;
                }
            }
            //4.4 进行排序(根据是否实现PriorityOrdered、Ordered接口和order值来排序)
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            registryProcessors.addAll(currentRegistryProcessors);
            //4.5 遍历currentRegistryProcessors, 执行postProcessBeanDefinitionRegistry方法
            invokeBeanDefinitionRegistryPostProcessors(
                currentRegistryProcessors, registry);
            currentRegistryProcessors.clear();
        }

        //5. 调用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法(BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor)
        invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
        //6. 最后, 调用入参beanFactoryPostProcessors中的普通BeanFactoryPostProcessor的postProcessBeanFactory方法
        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    }

    else {
        // Invoke factory processors registered with the context instance.
        invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    }

    //到这里 , 入参beanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已经全部处理完毕,下面开始处理容器中的所有BeanFactoryPostProcessor
    //7. 找出所有实现BeanFactoryPostProcessor接口的类
    String[] postProcessorNames =
        beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

    // 用于存放实现了PriorityOrdered接口的BeanFactoryPostProcessor
    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = 
        new ArrayList<BeanFactoryPostProcessor>();
    // 用于存放实现了Ordered接口的BeanFactoryPostProcessor的beanName
    List<String> orderedPostProcessorNames = new ArrayList<String>();
    // 用于存放普通BeanFactoryPostProcessor的beanName
    List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
    // 7.1 遍历postProcessorNames, 将BeanFactoryPostProcessor按实现PriorityOrdered、实现Ordered接口、普通三种区分开
    for (String ppName : postProcessorNames) {
        if (processedBeans.contains(ppName)) {
        	//7.2 跳过已经执行过的
        } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            //7.3 添加实现了PriorityOrdered接口的BeanFactoryPostProcessor
            priorityOrderedPostProcessors.add(beanFactory.getBean(
                ppName, BeanFactoryPostProcessor.class));
        } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            //7.4 添加实现了Ordered接口的BeanFactoryPostProcessor的beanName
            orderedPostProcessorNames.add(ppName);
        } else {
            //7.5 添加剩下的普通BeanFactoryPostProcessor的beanName
            nonOrderedPostProcessorNames.add(ppName);
        }
    }

    //8. 调用所有实现PriorityOrdered接口的BeanFactoryPostProcessor
    //8.1 对priorityOrderedPostProcessors排序
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    //8.2 遍历priorityOrderedPostProcessors, 执行postProcessBeanFactory方法
    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

    //9. 调用所有实现Ordered接口的BeanFactoryPostProcessor
    List<BeanFactoryPostProcessor> orderedPostProcessors = 
        new ArrayList<BeanFactoryPostProcessor>();
    for (String postProcessorName : orderedPostProcessorNames) {
        //9.1 获取postProcessorName对应的bean实例, 添加到orderedPostProcessors, 准备执行
        orderedPostProcessors.add(beanFactory.getBean(
            postProcessorName, BeanFactoryPostProcessor.class));
    }
    //9.2 对orderedPostProcessors排序
    sortPostProcessors(orderedPostProcessors, beanFactory);
    //9.3 遍历orderedPostProcessors, 执行postProcessBeanFactory方法
    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

    //10. 调用所有剩下的BeanFactoryPostProcessor
    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = 
        new ArrayList<BeanFactoryPostProcessor>();
    for (String postProcessorName : nonOrderedPostProcessorNames) {
        //10.1 获取postProcessorName对应的bean实例, 添加到nonOrderedPostProcessors, 准备执行
        nonOrderedPostProcessors.add(beanFactory.getBean(
            postProcessorName, BeanFactoryPostProcessor.class));
    }
    //10.2 遍历nonOrderedPostProcessors, 执行postProcessBeanFactory方法
    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

    //12. 清除元数据缓存(mergedBeanDefinitions、allBeanNamesByType、singletonBeanNamesByType),因为后处理器可能已经修改了原始元数据,例如, 替换值中的占位符...
    beanFactory.clearMetadataCache();
}

参考文章:https://blog.csdn.net/v123411739/article/details/87741251

代码看似很多,但是逻辑不复杂。就是将BeanDefinitionRegistryPostProcessor优先级从高到低的Bean依次执行;接着将FactoryPostProcessor优先级从高到低的Bean依次执行。

FactoryPostProcessor应用场景

FactoryPostProcessor典型的使用场景之一是:占位符替换。

在Spring中,可以通过配置外部配置文件,可以在Spring配置文件中使用占位符进行动态配置。即:通过引用配置文件中的key,在运行时,Spring再讲外部配置文件中key对应的value进行替换占位符,从而达到动态配置的功能。

在Spring中,org.springframework.context.support.PropertySourcesPlaceholderConfigurer就用于替换Spring容器中的BeanDefinition的相关属性值中的占位符进行替换。其大致流程为:PropertySourcesPlaceholderConfigurer读取Spring的Environment中的配置信息(包含系统配置信息、JVM配置信息、外部配置文件配置信息),然后将BeanDefinition的相关属性值中的占位符进行替换。从而达到动态配置的作用。

BeanDefinitionRegistryPostProcessor

对于上面描述中“将BeanDefinitionRegistryPostProcessor优先级从高到低的Bean依次执行;接着将FactoryPostProcessor优先级从高到低的Bean依次执行。”,BeanDefinitionRegistryPostProcessor又有什么作用呢?

/**
扩展到标准的{@link BeanFactoryPostProcessor} SPI,允许在常规的之前注册进一步的bean定义BeanFactoryPostProcessor检测开始起作用。特别是,BeanDefinitionRegistryPostProcessor可以注册更多的bean定义,这些定义又定义了BeanFactoryPostProcessor实例。
*/
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {

    /*
    在应用程序上下文的标准初始化之后修改它的内部bean定义注册表。所有常规bean定义都已加载,
    但还没有实例化任何bean。这允许在下一个后处理阶段开始之前添加更多的bean定义。
    */
    void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) 
        throws BeansException;

}

从上面的描述来看,BeanDefinitionRegistryPostProcessor是对BeanFactoryPostProcessor的一个扩展,BeanDefinitionRegistryPostProcessor除了像BeanFactoryPostProcessor可以在应用程序上下文的标准初始化之后修改它的内部bean工厂外,还可以往Bean工厂中新增bean。

编写BeanDefinitionRegistryPostProcessor实现类

public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		// TODO
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        RootBeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
        MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();

        PropertyValue pv1 = new PropertyValue("name", "zhangsan");
        PropertyValue pv2 = new PropertyValue("age", "20");
        PropertyValue pv3 = new PropertyValue("sex", "man");
        mutablePropertyValues.addPropertyValue(pv1);
        mutablePropertyValues.addPropertyValue(pv2);
        mutablePropertyValues.addPropertyValue(pv3);

        registry.registerBeanDefinition("person", beanDefinition);
    }

}

编写Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean 
         class="com.spring.postprocessor.factory.MyBeanDefinitionRegistryPostProcessor"/>

</beans>

测试代码

public static void main(String[] args) {

    ClassPathXmlApplicationContext context = 
        new ClassPathXmlApplicationContext("SpringContext.xml");

    Person person = context.getBean(Person.class);
    System.out.println(person);

    context.close();
}

输出结果:

Person(name=zhangsan, age=20, sex=man)

在配置文件中虽然没有定义Person的配置信息,但是程序还能运行成功,并且输出了Person的相关信息。说明MyBeanDefinitionRegistryPostProcessor往Spring容器中注册了一个Person Bean。

MyBeanDefinitionRegistryPostProcessor的使用场景

在很多情况下,如果需要批量扫描一定规则,将符合规则的的Class加载到Spring容器中,那么可以使用此接口完成此功能。

如在Mybatis中,对于符合条件的Mapper,可以将其加载到Spring容器中,让Spring来管理Mybatis的Mapper。org.mybatis.spring.mapper.MapperScannerConfigurer就是做这样的逻辑操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值