内部スキャンノートの分析を--spring invokeBeanFactoryPostProcessorsは、2019年10月22日を解決します

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/qq_35262405/article/details/102692115

invokeBeanFactoryPostProcessors方法

早く春が分析されたスキャンの全体のプロセスのために、あなたはClassPathBeanDefinitionScannerの分析では、次の資料を参照することができます。

春中の的のClassPathBeanDefinitionScanner

ここでちょうど工程分析スキャンするinvokeBeanFactoryPostProcessors方法
ここに画像を挿入説明
この方法では、スプリングの主達成後処理ビーンファクトリメソッドのコールバックは、スプリングビーンファクトリポストプロセッサは、 2つのカテゴリに分類されています

  1. BeanFactoryPostProcessor
  2. BeanDefinitionRegistryPostProcessor

前記BeanDefinitionRegistryPostProcessorサブクラスがBeanFactoryPostProcessorです

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

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

	//这个bean工厂是DefaultListableBeanFactory,DefaultListableBeanFactory同样实现了BeanDefinitionRegistry
	if (beanFactory instanceof BeanDefinitionRegistry) {
		//这个bean工厂同样也是一个注册器
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

		//这个list要放的是使用者自定义的BeanFactoryPostProcessor的是实现
		List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();

		//BeanDefinitionRegistryPostProcessor同样也是spring的扩展点之一,同时也是BeanFactoryPostProcessor的子类
		//使用者也可以实现这个BeanDefinitionRegistryPostProcessor接口来做一些增强
		//这个list要放的是使用者自定义的BeanDefinitionRegistryPostProcessor的是实现
		List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

		//判断传进来的对象是属于实现了哪一个接口的实现类,并加入对应的set集合
		//循环遍历使用者自定义的BeanFactoryPostProcessor的实现
		for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
			if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
				BeanDefinitionRegistryPostProcessor registryProcessor =
						(BeanDefinitionRegistryPostProcessor) postProcessor;
				// 这里直接执行使用者通过api传进来的BeanDefinitionRegistryPostProcessor实现类中的方法
				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.
		// 这里又创建了一个set集合,存放的同样是BeanDefinitionRegistryPostProcessor
		// 因为不只有使用者会实现BeanDefinitionRegistryPostProcessor
		// 这个集合是装的spring内部自己是实现的BeanDefinitionRegistryPostProcessor的对象
		// 为了区分又创建了一个set集合来存储
		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);

		//将用户的BeanDefinitionRegistryPostProcessor实现类和spring自己的是实现类的集合做了合并
		registryProcessors.addAll(currentRegistryProcessors);

		/**
		 * 完成扫描
		 * 这里将所有的BeanDefinitionRegistryPostProcessor的实现和注册器传了过去
		 * 这个方法也执行了所有的BeanDefinitionRegistryPostProcessor(包括自定义的和spring内部的)中实现的方法
		 * 十分重要
		 * 完成了绝大部分的bd的注册
		 */
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);

		// 清除list集合,节省空间
		currentRegistryProcessors.clear();

		// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
		// 找到所有实现Order接口的BeanDefinitionRegistryPostProcessors
		// 这个Order接口是用来决定执行顺序的
		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;
		// 这里之所以会需要循环是因为有可能在回调postProcessBeanDefinitionRegistry方法的时候可能会新注册BeanDefinitionRegistryPostProcessor的bean对象
		// 这是为了保障找到所有BeanDefinitionRegistryPostProcessor的实现类
		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.
		/**
		 * 这里开始执行BeanFactoryPostProcessors的实现类的回调方法
		 * 注意这里执行了两遍是因为registryProcessors虽然是BeanDefinitionRegistryPostProcessor的实现类
		 * 但是同样也是BeanFactoryPostProcessors的子类,所以同样也实现了BeanFactoryPostProcessors的方法
		 * 所以也需要调用
		 * 后面的invokeBeanFactoryPostProcessors方法则是传入BeanFactoryPostProcessor的实现类
		 */
		invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
	}

	else {
		// Invoke factory processors registered with the context instance.
		invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
	}
	/**
	 * 后面就是对所有BeanFactoryPostProcessor实现类(除了使用者通过api注入的)执行回调方法
	 */

	// Do not initialize FactoryBeans here: We need to leave all regular beans
	// uninitialized to let the bean factory post-processors apply to them!
	// 找到所有BeanFactoryPostProcessor实现类
	String[] postProcessorNames =
			beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

	// 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
		}
		// 实现了PriorityOrdered接口的
		else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
		}
		// 实现类Ordered接口的
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			orderedPostProcessorNames.add(ppName);
		}
		// 没有实现排序接口的
		else {
			nonOrderedPostProcessorNames.add(ppName);
		}
	}

	// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
	// 首先排序并执行实现了PriorityOrdered接口的实现类
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	// 回调postProcessBeanFactory方法
	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
	// 接下来排序并执行实现了Ordered接口的实现类
	List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
	for (String postProcessorName : orderedPostProcessorNames) {
		orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
	}
	sortPostProcessors(orderedPostProcessors, beanFactory);
	// 回调postProcessBeanFactory方法
	invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

	// Finally, invoke all other BeanFactoryPostProcessors.
	// 最后执行剩余的BeanFactoryPostProcessor实现类的回调方法postProcessBeanFactory
	List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
	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();
}

この方法では、以下の主な手順は完了します

  1. まず、ユーザがBeanDefinitionRegistryPostProcessor BeanFactoryPostProcessor実装クラスに渡され得る、と2つのリストに入れた、regularPostProcessorsはBeanFactoryPostProcessorの実現を保存され、registryProcessorsは、ストレージのBeanDefinitionRegistryPostProcessorを達成しました。プロセッサを介して、ユーザ定義の注釈を注入するが、APIメソッドを介して注入されていないことに留意されたいです。することによりAnnotationConfigApplicationContext.addBeanFactoryPostProcessor()、注射の方法。同時に、直接のコールバックBeanDefinitionRegistryPostProcessor実装クラスを見つけるpostProcessBeanDefinitionRegistry方法を。
  2. 来年の春には、実際には内部のバネが実装クラスを注入されたが、非常に重要なのは、このオブジェクトがいるところだけを見つけるためにBeanDefinitionRegistryPostProcessor実装クラスの内部に注入されていますConfigurationClassPostProcessor、それが出てくる春を完了するために多くの作業ですコレクションにcurrentRegistryProcessors。
  3. BeanDefinitionRegistryPostProcessorインターフェイスは、次に実行される、重合currentRegistryProcessorsにBeanDefinitionRegistryPostProcessor実装クラス内のすべてのセットを実装し、ユーザ自身のスプリング実装クラスpostProcessBeanDefinitionRegistry、ConfigurationClassPostProcessorに完成されたスプリングの内部に実装されたコールバック・クラス・メソッドを、(メソッドコールバック)をスキャン豆。
  4. 空集合は、その後、マップは、全てのBD BeanDefinitionRegistryPostProcessorクラスから取得するcurrentRegistryProcessorsは、実装されている事実に並べインターフェースコールバックは、バックの実行順序を決定する順序インタフェース、プロセッサ及びregistryProcessors currentRegistryProcessorsにこれらの目的を達成しながらセットは、プロセッサは、コールバックの実行postProcessBeanDefinitionRegistry方法。
  5. これらのオブジェクトは、セットプロセッサとregistryProcessors currentRegistryProcessorsに、currentRegistryProcessors空集合、残りBeanDefinitionRegistryPostProcessorマップ実装クラスから取得し続け、次いで、プロセッサは、次に、コールバックの実行postProcessBeanDefinitionRegistry方法。なお、新規のBeanに登録される可能性がありますサイクルにあっ必要があるためである理由は、すべてのBeanDefinitionRegistryPostProcessor実装クラスを見つけることを保証することである、BeanDefinitionRegistryPostProcessorコールバックメソッドpostProcessBeanDefinitionRegistry時間オブジェクト。
  6. 最後の文が実行され、registryProcessorsのregularPostProcessorsプロセッサセットの場合れるpostProcessBeanFactory最初のクラスBeanDefinitionRegistryPostProcessorを達成するために実行される方法、postProcessBeanFactory方法、BeanFactoryPostProcessor再実行postProcessBeanFactory方法。
  7. 最後に、すべてのコールバックメソッドを均等PriorityOrderedインタフェースを達成するために、3つのカテゴリーに分類され、そしてインターフェイスはソート順序インターフェース実装を実装していない(注射APIを使用してユーザを除く)BeanFactoryPostProcessor実装クラスです。

概要

  • この方法は、BeanFactoryPostProcessorsに主であるとBeanDefinitionRegistryPostProcessorは、コールバックメソッドの実装クラスを呼び出すことです。
  • APIの実装クラスを含むAnnotationConfigApplicationContext.addBeanFactoryPostProcessor()注射BeanFactoryPostProcessors BeanDefinitionRegistryPostProcessorと実装クラス、およびスプリングBeanDefinitionRegistryPostProcessor BeanFactoryPostProcessors実装クラスの内部、及びスキャンBeanDefinitionRegistryPostProcessor BeanFactoryPostProcessors実装クラスを。
  • 彼らのコールバックメソッドは、次の順序で実行します
  1. APIのAnnotationConfigApplicationContext.addBeanFactoryPostProcessor()BeanDefinitionRegistryPostProcessor注入の実装クラスpostProcessBeanDefinitionRegistryメソッド。
  2. 春BeanDefinitionRegistryPostProcessor内部クラスが実装postProcessBeanDefinitionRegistryする方法を。
  3. スキャンBeanDefinitionRegistryPostProcessor実装クラス、およびインタフェースの順序を実装は、実行postProcessBeanDefinitionRegistry方法。
  4. 他のすべての残りのBeanDefinitionRegistryPostProcessor実装クラス、実行postProcessBeanDefinitionRegistry方法。
  5. APIによってAnnotationConfigApplicationContext.addBeanFactoryPostProcessor()クラスの実装クラスBeanDefinitionRegistryPostProcessorは、ばねBeanDefinitionRegistryPostProcessor実装クラスの内部に注入し、走査BeanDefinitionRegistryPostProcessorが実装postProcessBeanFactory方法。
  6. APIによってAnnotationConfigApplicationContext.addBeanFactoryPostProcessor()BeanFactoryPostProcessors注入のpostProcessBeanFactory方法。
  7. スキャン実装クラスとインナー春とPriorityOrdered BeanFactoryPostProcessors出postProcessBeanFactory方法。
  8. スキャン実装クラスとインナー春と順序BeanFactoryPostProcessors出postProcessBeanFactory方法。
  9. そして、残りの実装クラスBeanFactoryPostProcessorsが内部飛び出すスキャンするpostProcessBeanFactory方法。

おすすめ

転載: blog.csdn.net/qq_35262405/article/details/102692115