com.github.datalking.context.support.PostProcessorRegistrationDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of play-mvc Show documentation
Show all versions of play-mvc Show documentation
simple mvc framework based on java servlet.
The newest version!
package com.github.datalking.context.support;
import com.github.datalking.aop.aspectj.AnnotationAwareAspectJAutoProxyCreator;
import com.github.datalking.beans.factory.config.BeanFactoryPostProcessor;
import com.github.datalking.beans.factory.config.BeanPostProcessor;
import com.github.datalking.beans.factory.config.ConfigurableListableBeanFactory;
import com.github.datalking.beans.factory.support.BeanDefinitionRegistry;
import com.github.datalking.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import com.github.datalking.common.Ordered;
import com.github.datalking.common.PriorityOrdered;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* BeanFactoryPostProcessor相关功能执行的代理类
* 一是触发beanFactoryPostProcessor,二是注册beanFactoryPostProcessor
*
* @author yaoo on 4/13/18
*/
public class PostProcessorRegistrationDelegate {
public static void invokeBeanFactoryPostProcessors(
Collection extends BeanFactoryPostProcessor> postProcessors,
ConfigurableListableBeanFactory beanFactory) {
for (BeanFactoryPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessBeanFactory(beanFactory);
}
}
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory,
List beanFactoryPostProcessors) {
Set processedBeans = new HashSet<>();
/// 如果beanFactory是可注册BeanDefinition的,则处理实现了BeanDefinitionRegistryPostProcessor接口的bean
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// 遍历BeanDefinitionMap和singletonObjects,获取BeanDefinitionRegistryPostProcessor类型的bean名称
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class);
// 实例化默认的后置处理器,包括ConfigurationClassPostProcessor
List priorityOrderedPostProcessors = new ArrayList<>();
for (String ppName : postProcessorNames) {
priorityOrderedPostProcessors.add((BeanDefinitionRegistryPostProcessor) beanFactory.getBean(ppName));
processedBeans.add(ppName);
}
// sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
// 通过ConfigurationClassPostProcessor扫描beanDefinitionMap中带有@Configuration的类,再进一步扫描@Bean、@ComponentScan
// 注册AnnotationAwareAspectJAutoProxyCreator的BeanDefinition
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
}
/// 如果beanFactory不可注册BeanDefinition
else {
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
// 获取上面过程中新增的BeanFactoryPostProcessor,如PlaceholderConfigurerSupport,也包含已处理过的
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class);
List postProcessors = new ArrayList<>();
for (String postProcessorName : postProcessorNames) {
/// 若已经处理过,则跳过
if (!processedBeans.contains(postProcessorName)) {
postProcessors.add((BeanFactoryPostProcessor) beanFactory.getBean(postProcessorName));
}
}
invokeBeanFactoryPostProcessors(postProcessors, beanFactory);
}
private static void invokeBeanDefinitionRegistryPostProcessors(
Collection extends BeanDefinitionRegistryPostProcessor> postProcessors,
BeanDefinitionRegistry registry) {
for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessBeanDefinitionRegistry(registry);
}
}
public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory,
AbstractApplicationContext applicationContext) {
// 获取BeanPostProcessor类型的bean名
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class);
List priorityOrderedPostProcessors = new ArrayList<>();
List orderedPostProcessorNames = new ArrayList<>();
List nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
BeanPostProcessor pp = (BeanPostProcessor) beanFactory.getBean(ppName);
priorityOrderedPostProcessors.add(pp);
} else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
} else {
nonOrderedPostProcessorNames.add(ppName);
}
}
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// 下面实例化 AutoProxyCreator
List orderedPostProcessors = new ArrayList<>();
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = (BeanPostProcessor) beanFactory.getBean(ppName);
orderedPostProcessors.add(pp);
// 特殊初始化AnnotationAwareAspectJAutoProxyCreator的bean
if (pp instanceof AnnotationAwareAspectJAutoProxyCreator) {
((AnnotationAwareAspectJAutoProxyCreator) pp).setBeanFactory(beanFactory);
}
}
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
List nonOrderedPostProcessors = new ArrayList();
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = (BeanPostProcessor) beanFactory.getBean(ppName);
nonOrderedPostProcessors.add(pp);
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
}
private static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory,
List postProcessors) {
for (BeanPostProcessor postProcessor : postProcessors) {
beanFactory.addBeanPostProcessor(postProcessor);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy