栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring beanFactory后处理器之BeanDefinitionRegistryPostProcessor原理解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring beanFactory后处理器之BeanDefinitionRegistryPostProcessor原理解析


以上是spring官方文档的描述,翻译过来BeanDefinitionRegistryPostProcessor其实就是用来注册BeanDefinition的。
我们都知道,如果为了让spring容器能够实例化各个Bean,就必须先有对应的BeanDefinition,所以这个接口就是为了这个作用的。

其中大名鼎鼎的BeanFactoryPostProcessor的实现类ConfigurationClassPostProcessor也实现了这个接口。

怎么用?

这里以spring boot应用为例

定义BeanDefinitionRegistryPostProcessor 实现类

@Component
public class CustomizeBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(registry.getClass()).getBeanDefinition();
        registry.registerBeanDefinition(BeanFactoryRegistryObject.class.getName(), beanDefinition);
    }

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

很简单,我定义了一个BeanFactoryRegistryObject类,这里就是注册这个自定义类,
最终BeanFactoryRegistryObject会被spring容器管理,spring容器会自动帮我们生成实例。

原理解析

org.springframework.context.support.AbstractApplicationContext#refresh

@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);

spring启动会进入到这个refresh方法,其中invokeBeanFactoryPostProcessors,从方法名也猜到是执行BeanFactoryPostProcessors的

	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()));
		}
	}

其中PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())方法,别看它只是一个静态方法,它可是spring的核心处理入口之一,我们进入方法内部

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

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

		if (beanFactory instanceof BeanDefinitionRegistry) {
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			List regularPostProcessors = new ArrayList<>();
			List 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 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();
			}
			// 省略部分代码
	}

其中入参beanFactoryPostProcessors是一系列的BeanFactoryPostProcessors,这里先不管,我们知道用 元素或者使用@ComponentScan注解spring会隐式注册以下后处理器:
ConfigurationClassPostProcessor
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
EventListenerMethodProcessor

springboot应用启动后,会调用以下方法,注册上面几个处理器:
org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.Object)

好了,回到上面的方法,发现里面有好几处以下代码:

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);
				}
			}

在干什么,为什么要重复调用,是不是搞错了?这个肯定不是,毕竟spring可以说是神一样的存在,会让如此低级的错误出现吗,不可能。
这里是为了拿继承了BeanDefinitionRegistryPostProcessor的BeanFactoryPostProcessor
然后逐个去调用其postProcessBeanDefinitionRegistry方法。
为什么调用这么多次?
第一次调用
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.

数组里面是ConfigurationClassPostProcessor这个beanFactory后处理器,它是干什么的?
可以这样说,我们所有配置类的实例化前的准备工作(配置类是指包含注解Component、Controller、Service等)以及spring boot所有自动注入的配置都得靠它。因为它负责扫描所有配置类,以及所有被import配置类,最终统一放到一个叫做beanDefinitionMap里面,这里就不细展开,总之,可以理解它是spring ioc前期准备数据的就行。

第一轮调用后,所有配置类信息包含spring boot自动依赖的都已生成好,但是还没实例化,只是生成了BeanDefinition,放到本地缓存。
如下:

开始第二次调用
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.

第二次拿到了上面我自定义的CustomizeBeanFactoryPostProcessor类,为什么第二次才拿到,因为第一次ConfigurationClassPostProcessor这个已经帮我们把所有配置类加载到Map,其中getBeanNamesForType方法就是从beanDefinitionNames遍历去找BeanDefinitionRegistryPostProcessor,所以第二次才能拿到。
注意:这里还不会去执行invokeBeanDefinitionRegistryPostProcessors方法,因为我没有实现Ordered这个接口。
下面也可以看到,Map数量还是跟第一次一样:138个,说明类BeanFactoryRegistryObject没有注册进去。

第三次调用

发现beanDefinitionMap多了一个,多出来的就是BeanFactoryRegistryObject类,到此可以实现注入自定义的类,为后续让spring帮我们实例化做好准备。

这里有个疑问,为什么最后一次调用是while 循环?
从spring给的注释:
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
也可以看出来,这是为了能够将所有BeanDefinitionRegistryPostProcessors 类都全部执行一次invokeBeanDefinitionRegistryPostProcessors方法。

为什么每一次循环都再去调getBeanNamesForType方法去找呢?打个比方,假如我上面自定义的BeanFactoryRegistryObject这个类也是一个BeanDefinitionRegistryPostProcessors ,那么是不是循环后回来再调一次getBeanNamesForType方法就拿到了,是不是?spring就是这样考虑的,很巧妙吧。

到此,BeanDefinitionRegistryPostProcessor介绍全部结束。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424457.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号