第五章 spring-connet之核心類PostProcessorRegistrationDelegate與BeanFactoryPostProcessor體系

前言

前面講了reser方法,java

PostProcessorRegistrationDelegate核心方法

  1. invokeBeanFactoryPostProcessors

    執行BeanDefinitionRegistry接口與 BeanDefinitionRegistryPostProcessor接口的實現類spring

  2. registerBeanPostProcessors

    BeanDefinitionRegistry的子類進行排序mybatis

invokeBeanFactoryPostProcessors解讀

  1. 執行invokeBeanFactoryPostProcessors方法形參beanFactoryPostProcessors裏面的BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
  2. 執行beanFactory裏面的BeanDefinitionRegistryPostProcessor實現類的postProcessBeanDefinitionRegistry的postProcessBeanDefinitionRegistry方法
  3. 循環獲得beanFactory裏面的BeanDefinitionRegistryPostProcessor實現類的postProcessBeanDefinitionRegistry方法
  4. 在執行BeanDefinitionRegistryPostProcessor的實現類的postProcessBeanFactory方法
  5. 在執行invokeBeanFactoryPostProcessors方法形參beanFactoryPostProcessors裏面的BeanFactoryPostProcessor實現類的postProcessBeanFactory方法
  6. 執行beanFactory裏面的BeanFactoryPostProcessor實現類的postProcessBeanFactory方法
  7. 進行排序在執行實現類,排序分類是先對實現PriorityOrdered的類進行排序並執行,在對實現Ordered的類進行排序並執行,最後對其餘類執行。
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;
			List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>();
					//  執行invokeBeanFactoryPostProcessors方法形參beanFactoryPostProcessors裏面的BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
			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<BeanDefinitionRegistryPostProcessor>();

			// 執行beanFactory裏面的BeanDefinitionRegistryPostProcessor與PriorityOrdered實現類的postProcessBeanDefinitionRegistrypostProcessBeanDefinitionRegistry方法
			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();

			// 執行beanFactory裏面的BeanDefinitionRegistryPostProcessor與Ordered實現類的postProcessBeanDefinitionRegistrypost方法
			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();

			// 循環獲得beanFactory裏面的BeanDefinitionRegistryPostProcessor實現類的postProcessBeanDefinitionRegistry方法。會排除已經執行的對象
			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();
			}

			//  在執行BeanDefinitionRegistryPostProcessor的實現類的postProcessBeanFactory方法
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			// 在執行invokeBeanFactoryPostProcessors方法形參beanFactoryPostProcessors裏面的BeanFactoryPostProcessor實現類的postProcessBeanFactory方法
			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);

		// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
		List<String> orderedPostProcessorNames = new ArrayList<String>();
		List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
		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);

		// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
		List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
		for (String postProcessorName : orderedPostProcessorNames) {
			orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		sortPostProcessors(orderedPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

		// Finally, invoke all other BeanFactoryPostProcessors.
		List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
		for (String postProcessorName : nonOrderedPostProcessorNames) {
			nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

		// Clear cached merged bean definitions since the post-processors might have
		// modified the original metadata, e.g. replacing placeholders in values...
		beanFactory.clearMetadataCache();
	}

BeanFactoryPostProcessor體系

BeanFactoryPostProcessor體系主要是由BeanFactoryPostProcessor與子接口BeanDefinitionRegistryPostProcessor構成app

BeanFactoryPostProcessor能夠對ConfigurableListableBeanFactory對象進行操做。post

public interface BeanFactoryPostProcessor {

	void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}
  1. BeanDefinitionRegistryPostProcessor調用BeanDefinitionRegistry.registerBeanDefinition的方法註冊BeanDefinition
  2. BeanDefinitionRegistryPostProcessor調用BeanDefinitionRegistry.registerBeanDefinition的方法註冊BeanDefinition
  3. BeanDefinitionRegistryPostProcessor調用BeanDefinitionRegistry.registerBeanDefinition的方法註冊BeanDefinition
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {

	void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

}

上面有一句這樣的話:循環獲得beanFactory裏面的BeanDefinitionRegistryPostProcessor實現類的postProcessBeanDefinitionRegistry方法。會排除已經執行的對象。你們是否是能難理解。好比BeanDefinitionRegistryPostProcessor的實現類A向BeanDefinitionRegistry註冊了BeanDefinitionRegistryPostProcessor的實現類B.而後B註冊了C。那B,C如何被執行。能夠經過上面標註的循環的代碼就能夠解決了。rest

基於BeanDefinitionRegistryPostProcessor實現spring 掃描默認路徑下的class

註冊ConfigurationClassPostProcessor

AnnotationConfigUtils的registerAnnotationConfigProcessors會向BeanDefinitionRegistry註冊ConfigurationClassPostProcessor。code

public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable Object source) {
		.......
		Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(4);

		if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
		}
		.......
		return beanDefs;
	}
調用鏈

AnnotationConfigApplicationContext,AnnotationConfigServletWebServerApplicationContext,AnnotationConfigReactiveWebServerApplicationContext都是默認的註解app上下文容器,全部的AnnotationConfigs都會默認註冊ConfigurationClassPostProcessor。server

總結

  1. PostProcessorRegistrationDelegate 是applincationContext最關鍵的類之一
  2. BeanFactoryPostProcessor是整個applincationContext最核心的體系,mybatis的mapper,dubbo的server都是給予BeanFactoryPostProcessor體系實現
  3. 全部AnnotationConfig上線文都會註冊ConfigurationClassPostProcessor
相關文章
相關標籤/搜索