Spring 을 다 먹 은 BeanFactory PostProcessors.
Spring 대관원, 내 가 곤 혹 스 러 웠 던 것 은 아마 너 도 있 을 것 이다!
springboot 주해 처리 프로 세 스 노트 에서 BeanFactory PostProcessor 를 대체적으로 설명 한 적 이 있 으 며, 주 해 를 처리 하 는 핵심 클래스
ConfigurationClassPostProcessor
가 Annotated Bean DefinitionReader 구조 함수 에 용기 에 등록 되 어 invokeBeanFactoryPostProcessors
방법 에서 호출 되 었 다 고 언급 했다.다음 소스 코드 를 읽 기 전에 springboot 주석 처리 절차 와 BeanFactory PostProcessor 의 역할 을 살 펴 보고 더욱 전면적 인 인식 을 가지 고 소스 코드 를 이해 하 는 데 도움 을 주 는 것 이 좋 습 니 다.핵심 클래스 ConfigurationClassPostProcessor 의 post ProcessBeanDefinition Registry 호출 이 원본 주석 에서 두 번 째 파 를 봅 니 다.
여기 서
invokeBeanFactoryPostProcessors
방법의 소스 코드 를 정리 하기 시작 했다.springboot 프로젝트
invokeBeanFactoryPostProcessors
방법 이 실 행 될 때 들 어 오 는 매개 변 수 는?0 = {SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor@3818}
1 = {ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor@3819}
2 = {ConfigFileApplicationListener$PropertySourceOrderingPostProcessor@3820}
/**
* BeanFactoryPostProcessor BeanFactory . bean
* postProcessBeanDefinitionRegistry BeanFactoryPostProcessor , bean .
* :
* 1. postProcessBeanDefinitionRegistry BeanFactoryPostProcessor
* 2. PriorityOrdered Ordered
*
* beanFactoryPostProcessors PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
* , context BFPP api `ConfigurableApplicationContext#addBeanFactoryPostProcessor` .
* BeanDefinitionRegistryPostProcessor , `postProcessBeanDefinitionRegistry`, bean .
*
*/
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
// Bean
Set processedBeans = new HashSet<>();
/******** postProcessBeanDefinitionRegistry
// , context List
// BeanDefinitionRegistryPostProcessor
****************************************************************************************/
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// BeanFactoryPostProcessor, BeanDefinitionRegistryPostProcessor
List regularPostProcessors = new ArrayList<>();
// BeanDefinitionRegistryPostProcessor
List registryProcessors = new ArrayList<>();
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
// postProcessBeanDefinitionRegistry
// , context List
// BeanDefinitionRegistryPostProcessor
registryProcessor.postProcessBeanDefinitionRegistry(registry);
//
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
// FactoryBeans, factory post-processors 。
// 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.
// , , , registryProcessors , .
List currentRegistryProcessors = new ArrayList<>();
/******** postProcessBeanDefinitionRegistry */
// beanFactory PriorityOrdered BeanDefinitionRegistryPostProcessor
// ConfigurationClassPostProcessor postProcessBeanDefinitionRegistry
/****************************************************************************************/
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
// 2.beanFactory bean BeanDefinitionRegistryPostProcessor ,
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
// beanFactory bean BeanDefinitionRegistryPostProcessor , PriorityOrdered , , currentRegistryProcessors .
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//getBean , currentRegistryProcessors
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
// bean processedBeans .
processedBeans.add(ppName);
}
}
// currentRegistryProcessors PriorityOrdered , value
sortPostProcessors(currentRegistryProcessors, beanFactory);
// currentRegistryProcessors ( ) registryProcessors.
registryProcessors.addAll(currentRegistryProcessors);
// postProcessBeanDefinitionRegistry , springFactory BeanDefinitionRegistryPostProcessors
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// currentRegistryProcessors
currentRegistryProcessors.clear();
/******** postProcessBeanDefinitionRegistry
// beanFactory Ordered BeanDefinitionRegistryPostProcessor
// :
// PriorityOrdered Ordered , Ordered , PriorityOrdered Ordered
// BeanDefinitionRegistryPostProcessor bean PriorityOrdered Ordered
// beanFactory PriorityOrdered Ordered BeanDefinitionRegistryPostProcessor
// (processedBeans ),getBean currentRegistryProcessors , processedBeans ( );
// sortPostProcessors ,PriorityOrdered Ordered
// postProcessBeanDefinitionRegistry , bean
//
****************************************************************************************/
// 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();
/******** postProcessBeanDefinitionRegistry BeanDefinitionRegistryPostProcessor 。 。
//
// 1. beanFactory BeanDefinitionRegistryPostProcessor currentRegistryProcessors ,
// 2. sortPostProcessors currentRegistryProcessors , postProcessBeanDefinitionRegistry , bean , 1
// 3. , .
****************************************************************************************/
// 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.
/******** postProcessBeanFactory
// 1. registryProcessors BeanDefinitionRegistryPostProcessor postProcessBeanFactory
// 2. regularPostProcessors BeanFactoryPostProcessor postProcessBeanFactory ,
// regularPostProcessors appContext BeanFactoryPostProcessor,
// BeanFactoryPostProcessor
****************************************************************************************/
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!
/******** postProcessBeanFactory
// BeanFactoryPostProcessor
//
// PriorityOrdered priorityOrderedPostProcessors
// Ordered , orderedPostProcessorNames
// , nonOrderedPostProcessorNames
****************************************************************************************/
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List priorityOrderedPostProcessors = new ArrayList<>();
List orderedPostProcessorNames = new ArrayList<>();
List 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.
// PriorityOrdered priorityOrderedPostProcessors
// priorityOrderedPostProcessors , BeanFactoryPostProcessor
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
// Ordered ,
// orderedPostProcessorNames, , orderedPostProcessors ,
// orderedPostProcessors , BeanFactoryPostProcessor
List orderedPostProcessors = new ArrayList<>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// Finally, invoke all other BeanFactoryPostProcessors.
// ,
// nonOrderedPostProcessorNames, , nonOrderedPostProcessors ,
// nonOrderedPostProcessors , BeanFactoryPostProcessor
List 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();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.