简单回顾一下上一篇文章,是在BeanFacroty创建完之后,可以通过Editor和EditorRegistrar实现对类属性的自定义扩展,以及忽略要自动装配的Aware接口。
本篇帖子会顺着refresh()主流程方法接着向下执行。在讲invokeBeanFactoryPostProcessors方法的具体逻辑之前,先简单介绍BeanFactoryPostProcessor接口和整个invokeBeanFactoryPostProcessors方法的执行流程。
refresh
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
prepareRefresh();
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
prepareBeanFactory(beanFactory);
try {
postProcessBeanFactory(beanFactory);
invokeBeanFactoryPostProcessors(beanFactory);
}
}
}
@FunctionalInterface
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}
BFPP和BDRPP
根据上面的流程图以及代码块可以看出BFPP和BDRPP的关系,其中BFPP类中只有一个postProcessBeanFactory()方法,而BDRPP中只有postProcessBeanDefinitionRegistry()方法,因为是继承的关系,所以所有实现BDRPP接口的类,也会有postProcessBeanFactory()方法。
而BFPP和BDRPP其中有一个很重要的区别就在于,它们两个方法接收的参数不同,其中BFPP接收BeanFactory参数,针对整个BeanFactory做操作,而BDRPP接收的参数是BeanDefinitionRegistry(可以理解是对Definition做一些增删改查的操作)。
BeanDefinitionRegistry
public interface BeanDefinitionRegistry extends AliasRegistry {
/**
* 注册BeanDefinition到注册表
*/
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException;
/**
* 移除注册表中beanName的BeanDefinition
*/
void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
/**
* 获取注册表中beanName的BeanDefinition
*/
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
/**
* 检查此注册表是否包含具有给定名称的BeanDefinition
*/
boolean containsBeanDefinition(String beanName);
/**
* 返回此注册表中定义的所有bean的名称
*/
String[] getBeanDefinitionNames();
/**
* 返回注册表中定义的bean的数目
*/
int getBeanDefinitionCount();
/**
* 确定给定bean名称是否已在该注册表中使用
*/
boolean isBeanNameInUse(String beanName);
}
接口的作用和整体的执行流程已经介绍完成,下面看看invokeBeanFactoryPostProcessors方法的具体执行逻辑。
invokeBeanFactoryPostProcessors
主要是调用delegate中的同名invokeBeanFactoryPostProcessors方法,对BaenFactory中和自定义注册进来的BFPP的实现类进行处理。
其中getBeanFactoryPostProcessors()会获取自定义扩展继承了BFPP和BDRPP的类。同时,也会先处理这部分的BFPP。
invokeBeanFactoryPostProcessors
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
// 获取到当前应用程序上下文的beanFactoryPostProcessors变量的值,并且实例化调用执行所有已经注册的beanFactoryPostProcessor
// 默认情况下,通过getBeanFactoryPostProcessors()来获取已经注册的BFPP,但是默认是空的
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
具体处理逻辑
整个invokeBeanFactoryPostProcessors()方法可以分为几块来看
- 先处理参数beanFactoryPostProcessors中自定义的BFPP和BDRPP。
//优先处理入参中的beanFactoryPostProcessors,遍历所有beanFactoryPostProcessors, //并将参数中的BFPP和BDRPP区分开 for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { //如果是BDRPP类型 if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; //直接调用BDRPP中的postProcessBeanDefinitionRegistry具体方法进行处理 registryPostProcessor.postProcessBeanDefinitionRegistry(registry); //放入registryPostProcessors集合中 registryPostProcessors.add(registryPostProcessor); } else { //否则,只是普通的BeanFactoryPostProcessor,则放入regularPostProcessors集合 regularPostProcessors.add(postProcessor); } }
- 获取所有实现BeanDefinitionRegistryPostProcessor类型的beanName,并筛选出继承了PriorityOrdered类的,排序后执行PPBDR方法。
// 用于保存本次要执行的BeanDefinitionRegistryPostProcessor List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. // 调用所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor实现类 // 找到所有实现BeanDefinitionRegistryPostProcessor接口bean的beanName String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); // 遍历处理所有符合规则的postProcessorNames for (String ppName : postProcessorNames) { // 检测是否实现了PriorityOrdered接口 if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { // 获取名字对应的bean实例,添加到currentRegistryProcessors中 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行 processedBeans.add(ppName); } } // 按照优先级进行排序操作 sortPostProcessors(currentRegistryProcessors, beanFactory); // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法 registryProcessors.addAll(currentRegistryProcessors); // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); // 执行完毕之后,清空currentRegistryProcessors currentRegistryProcessors.clear();
- 再次获取所有实现BeanDefinitionRegistryPostProcessor类型的beanName,找出未执行过并继承了Ordered接口的,排序执行PPBDR方法。
//再次获取BDRPP类型的所有postProcessorNames postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); //实现了Order接口的BeanDefinitionRegistryPostProcessor放入该集合 List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>(); for (String ppName : postProcessorNames) { //如果是没执行过,并且实现了Order接口的 if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { //放到Order集合中 orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); //放入processedBeans集合,避免重复执行 processedBeans.add(ppName); } } //按照优先级排序 OrderComparator.sort(orderedPostProcessors); //放入registryPostProcessors集合 registryPostProcessors.addAll(orderedPostProcessors); //遍历orderedPostProcessors集合,执行postProcessBeanDefinitionRegistry方法 invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
- 循环遍历,找出所有继承BeanDefinitionRegistryPostProcessor并且尚未执行的beanName,排序执行
// 最后,调用所有剩下的BeanDefinitionRegistryPostProcessors boolean reiterate = true; while (reiterate) { reiterate = false; // 找出所有实现BeanDefinitionRegistryPostProcessor接口的类 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); // 遍历执行 for (String ppName : postProcessorNames) { // 跳过已经执行过的BeanDefinitionRegistryPostProcessor if (!processedBeans.contains(ppName)) { // 获取名字对应的bean实例,添加到currentRegistryProcessors中 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行 processedBeans.add(ppName); reiterate = true; } } // 按照优先级进行排序操作 sortPostProcessors(currentRegistryProcessors, beanFactory); // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法 registryProcessors.addAll(currentRegistryProcessors); // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); // 执行完毕之后,清空currentRegistryProcessors currentRegistryProcessors.clear(); } //遍历registryPostProcessors和regularPostProcessors中有所的bean, //执行BFPP接口postProcessBeanFactory方法 invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
- 截止到这,已经将所有实现BeanDefinitionRegistryPostProcessor类型的类按照顺序执行了postProcessBeanDefinitionRegistry()方法。
上面有说到,如果类继承了BeanDefinitionRegistryPostProcessor那么它同时还会拥有BeanFactoryPostProcessor的postProcessBeanFactory()方法。 接下来只需要获取到所有实现BeanFactoryPostProcessor类型的类,并按照PriorityOrdered -> Ordered -> nonOrdered的顺序来执行方法即可。
//找到所有实现BeanFactoryPostProcessor的类
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
//依次声明priorityOrderedPostProcessors、orderedPostProcessorNames和nonOrderedPostProcessorNames分别用来存放对应
//实现了priorityOrdered、ordered和没实现排序接口的类
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
List<String> orderedPostProcessorNames = new ArrayList<String>();
List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
//遍历所有postProcessorNames,将没执行过的BFPP方法的类放入对应集合中。
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.
//按照优先级进行排序,并执行具体的BFPP方法
OrderComparator.sort(priorityOrderedPostProcessors);
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));
}
OrderComparator.sort(orderedPostProcessors);
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);
完整代码
class PostProcessorRegistrationDelegate {
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
//所有已经执行过BFPP的存储在processedBeans,防止重复执行
Set<String> processedBeans = new HashSet<String>();
//判断beanFactory是否属于BeanDefinitionRegistry类型,默认的beanFactory是DefaultListableBeanFactory,
//实现了BeanDefinitionRegistry 所以为true
if (beanFactory instanceof BeanDefinitionRegistry) {
//强制类型转换
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
//存放BFPP类型的集合
List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
//存放BDRPP类型的集合
List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =
new LinkedList<BeanDefinitionRegistryPostProcessor>();
//优先处理入参中的beanFactoryPostProcessors,遍历所有beanFactoryPostProcessors,
//并将参数中的BFPP和BDRPP区分开
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
//如果是BDRPP类型
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryPostProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
//直接调用BDRPP中的postProcessBeanDefinitionRegistry具体方法进行处理
registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
//放入registryPostProcessors集合中
registryPostProcessors.add(registryPostProcessor);
}
else {
//否则,只是普通的BeanFactoryPostProcessor,则放入regularPostProcessors集合
regularPostProcessors.add(postProcessor);
}
}
//根据type获取BeanFactory中所有类型为BDRPP的postProcessorNames
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
//将实现了BDRPP和priorityOrder接口的类放入该集合
List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
for (String ppName : postProcessorNames) {
//如果是PriorityOrdered类型的
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//放入priorityOrderedPostProcessors集合
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
//放入processedBeans集合,避免重复执行
processedBeans.add(ppName);
}
}
//按照优先级进行排序
OrderComparator.sort(priorityOrderedPostProcessors);
//添加到registryPostProcessors中
registryPostProcessors.addAll(priorityOrderedPostProcessors);
//遍历priorityOrderedPostProcessors集合,执行postProcessBeanDefinitionRegistry方法
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
//再次获取BDRPP类型的所有postProcessorNames
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
//实现了Order接口的BeanDefinitionRegistryPostProcessor放入该集合
List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
for (String ppName : postProcessorNames) {
//如果是没执行过,并且实现了Order接口的
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
//放到Order集合中
orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
//放入processedBeans集合,避免重复执行
processedBeans.add(ppName);
}
}
//按照优先级排序
OrderComparator.sort(orderedPostProcessors);
//放入registryPostProcessors集合
registryPostProcessors.addAll(orderedPostProcessors);
//遍历orderedPostProcessors集合,执行postProcessBeanDefinitionRegistry方法
invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
boolean reiterate = true;
while (reiterate) {
reiterate = false;
//找出所有实现了BDRPP接口的类
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
//跳过执行过BDRPP的类
if (!processedBeans.contains(ppName)) {
//根据name获取实例
BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
//添加到registryPostProcessors集合中
registryPostProcessors.add(pp);
//添加到processedBeans集合中
processedBeans.add(ppName);
//直接执行BDRPP
pp.postProcessBeanDefinitionRegistry(registry);
reiterate = true;
}
}
}
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
//遍历registryPostProcessors和regularPostProcessors中有所的bean,
//执行BFPP接口postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
//如果beanFactory不属于BeanDefinitionRegistry,直接执行具体方法
else {
// Invoke factory processors registered with the context instance.
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
//到这为止,入参的beanFactoryPostProcessors和BeanFactory中BDRPP类型的方法已经全部处理完成
//后面开始都是操作BFPP类型
//找到所有实现BeanFactoryPostProcessor的类
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
//依次声明priorityOrderedPostProcessors、orderedPostProcessorNames和nonOrderedPostProcessorNames分别用来存放对应
//实现了priorityOrdered、ordered和没实现排序接口的类
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
List<String> orderedPostProcessorNames = new ArrayList<String>();
List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
//遍历所有postProcessorNames,将没执行过的BFPP方法的类放入对应集合中。
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.
//按照优先级进行排序,并执行具体的BFPP方法
OrderComparator.sort(priorityOrderedPostProcessors);
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));
}
OrderComparator.sort(orderedPostProcessors);
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);
}
看完上面这段源码之后,不知道大家有没有这样的一个疑问,为什么重复代码较多?每次都要根据类型重新获取String[] postProcessorNames,第一次获取后,根据postProcessorNames按照PriorityOrdered和Order进行优先级排序、分组执行不行么?
是因为在执行BDRPP的方法postProcessBeanDefinitionRegistry时,有可能会有新增的额外的BDRPP的类,每次都重新获取,能避免BDRPP类执行的不完全。
流程图