接着上一篇文章走
postProcessBeanFactory
在 AbstractApplicationContext
中提供了給子類,在BeanFactory
實例化前後進行的操作。
invokeBeanFactoryPostProcessors
初始並實例化所有 的BeanFactoryPostProcessor,這一段代碼挺複雜,下面看看其代碼:
從 invokeBeanFactoryPostProcessors
進入,直接去到 PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
:
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>();
if (beanFactory instanceof BeanDefinitionRegistry) {
// 如果 factory類型爲 BeanDefinitionRegistry
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
// BeanDefinitionRegistryPostProcessor 類型的 postProcessor
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
// 首先先執行 BeanDefinitionRegistryPostProcessor 的接口方法。
registryProcessor.postProcessBeanDefinitionRegistry(registry);
// 而後加入到 registryProcessors 中。
registryProcessors.add(registryProcessor);
}
else {
// 普通的 postProcess
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<>();
// 首先,將所有實現優先級PriorityOrdered 的 BeanDefinitionRegistryPostProcessor先執行
// 獲取所有 BeanDefinitionRegistryPostProcessor 類型的bean。
// 默認會有一個 org.springframework.context.annotation.internalConfigurationAnnotationProcessor
// 即 ConfigurationClassPostProcessor
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
processedBeans.add(ppName);
}
}
// 根據beanFactory內部排序規則,給剛剛加入到 currentRegistryProcessors 進行排序。
sortPostProcessors(currentRegistryProcessors, beanFactory);
// 將其全部加入到 BeanDefinitionRegistryPostProcessor 專屬的集合 registryProcessors 中。
registryProcessors.addAll(currentRegistryProcessors);
// 先invoke 上面有實現 優先級中的 BeanDefinitionRegistryPostProcessor,即有優先級的先執行。
// 默認爲 有ConfigurationClassPostProcessor
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// 將 ConfigurationClassPostProcessor 清除
currentRegistryProcessors.clear();
// 將 BeanDefinitionRegistryPostProcessors 中,實現了 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);
// 將 剛找到的實現了 Ordered 的方法加入
registryProcessors.addAll(currentRegistryProcessors);
// 對其進行調用
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// 清楚當前調用者信息
currentRegistryProcessors.clear();
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
// 最後,將其他的 BeanDefinitionRegistryPostProcessor 類型給都調用了,知道沒有。
boolean reiterate = true;
while (reiterate) {
// 循環
reiterate = false;
// 每次都重新拿,所以是允許自定義的 對 BeanDefinitionRegistryPostProcessor 刪減。
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.
// 執行beanpost的其他類型方法即 postProcessBeanFactory
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
// 執行普通的 postProcessors 的 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!
// 獲取所有的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
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
// 實現了 PriorityOrdered 的類
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
// 實現了 Ordered 的類
orderedPostProcessorNames.add(ppName);
}
else {
// 沒有實現優先級的類。
nonOrderedPostProcessorNames.add(ppName);
}
}
// 優先執行,實現了 PriorityOrdered 的 BeanFactoryPostProcessors
// 排序
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 執行
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 執行 實現了 Ordered 的 BeanFactoryPostProcessors
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// 執行其他類型的 BeanFactoryPostProcessors
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
// 清除這個過程中部分臨時數據
beanFactory.clearMetadataCache();
Spring
通過各種不同的接入點,從而可以實現不同邏輯。同時也能夠將bean容器提供給其他框架使用。
invokeBeanFactoryPostProcessors
方法 則是 調用 BeanDefinitionRegistryPostProcessor
和 BeanFactoryPostProcessor
。
看看 這兩個接口功能:
BeanDefinitionRegistryPostProcessor
:是BeanFactoryPostProcessor
接口標準SPI實現。能在BeanFactoryPostProcessor
初始化之前 進行beanDefinition
的註冊。BeanFactoryPostProcessor
:允許用戶自定義操作去更改ApplicationContext
中的BeanDefinition
,但是不回影響到bean 實例。而如果 bean實例化的交互動作,則需要使用BeanPostProcessor
。
上面 invokeBeanFactoryPostProcessors
過程 可以有以下概括:
- 依次 將 所有 實現
PriorityOrdered
、Ordered
和 其他 的BeanDefinitionRegistryPostProcessor
進行實例化。
內置的爲ConfigurationClassPostProcessor
。
在 執行其他類型BeanDefinitionRegistryPostProcessor
時候,Spring 採用的是每次都重新去beanFactory
中獲取,所以可以在自定義類型的BeanDefinitionRegistryPostProcessor
增加 其他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();
}
- 依次執行
PriorityOrdered
、Ordered
、其他 的BeanFactoryPostProcessor
類型方法
默認內置的爲EventListenerMethodProcessor
。
Spring 中重要的 功能都是基於 PostProcessor 接入點實現,例如 @Configuration
等配置讀取,@Autowired
注入等。
接下來幾篇文章,將分析 初始化中重要的 PostProcessor
。
覺得博主寫的有用,不妨關注博主公衆號: 六點A君。
哈哈哈,一起研究Spring: