0717-7821348
关于我们

欢乐彩直播ios

您现在的位置: 首页 > 关于我们 > 欢乐彩直播ios
Spring5源码剖析(四)refresh办法
2019-11-04 22:01:14

点重视,不走失;继续更新Java相关技能及资讯!!!

之前Spring5源码剖析(三)refresh办法 现已讲到了refresh();中的postProcessBeanFactory(beanFactory);办法,现在接着读源码。

invokeBeanFactoryPostProcessors(beanFactory);

点开invokeBeanFactoryPostProcessors(beanFactory);源码:

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
// getBeanFactoryPostProcessors 获取的是 this.beanFactoryPostProcessors;
//this.beanFactoryPostProcessors 只能经过 AbstractApplicationContext.addBeanFactoryPostProcessor 办法增加
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}

getBeanFactoryPostProcessors()办法获取的是org.springframework.contextSpring5源码剖析(四)refresh办法.support.AbstractApplicationContext#beanFactoryPostProcessors 这个成员变量,这个成员变量只能经过代码中手动编码调用org.springframework.context.support.AbstractApplicationContext#addBeanFactoryPostProcessor办法来增加新的元素。很明显,咱们这儿为空。

该办法的首要的逻辑在PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());,点开源码:

public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set processedBeans = new HashSet<>();
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List regularPostProcessors = new ArrayList<>();
List registryProcessors = new ArrayList<>();
//beanFactoryPostProcessors是传进来里的目标,把传入的目标分类放入 BeanFactoryPostProcessor 和 BeanDefinitionRegistryPostProcessor
//BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor ,是一个特别的 BeanFactoryPostProcessor
for (BeanFactoryPostProcessor postProcessor : beanFactor科技苑yPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
//假如传入的beanFactoryPostProcessors是它的子类,即:BeanDefinitionRegistryPostProcessor
//则履行传入的BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry办法
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
} else {
regularPostProcessors.add(postProcessor);
}
}
// Do noitialize FactoryBeaSpring5源码剖析(四)refresh办法ns 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 currentRegistryProcessors = new ArrayList<>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement Spring5源码剖析(四)refresh办法PriorityOrdered.
//这儿只能拿到spring内部的BeanDefinitionRegistryPostProcessor,
//因为到这儿spring还没有去扫描Bean,获取不到咱们经过@Component标识的自定义BeanDefinitionRegistryPostProcessor
//一般默许情况下,这儿只要一个,BeanName:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
//对应的BeanClass:ConfigurationClassPostProcessor
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//beanFactory.getBean, 这儿开端创立BeanDefinitionRegistryPostProcessor bean 了
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
//排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
// registryProcessors 中放的是 BeanDefinitionRegistryPostProcessor
// 因为这Spring5源码剖析(四)refresh办法儿只履行eanDefinitionRegistryPostProcessor中独有的办法,而不会履行其父类即BeanFactoryProcessor的办法
// 所以这儿需求把处理器放入一个调集中,后续一致履行父类的办法
registryProcessors.addAll(currentRegistryProcessors);
// 履行BeanDefinitionRegistryPostProcessor,currentRegistryProcessors中放的是spring内部的BeanDefinitionRegistryPostProcessor
// 默许情况下,只要 org.springframework.context.annotation.ConfigurationClassPostProcessor
// ConfigurationClassPostProcessor 里边便是在履行扫描Bean,而且注册BeanDefinition
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// 清空这个暂时变量,便利后边再运用
currentRegistryProcessors.clear();
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
// 这儿现已能够获取到咱们经过注册到Spring容器的 BeanDefinitionRegistryPostProcessor 了
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
// 之前优先处理的是完成PriorityOrdered接口的,而PriorityOrdered接口也完成了Ordered接口
// 一切这儿需求把之前现已处理过的给过滤掉
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);
// 履行BeanDefinitionRegistryPostProcessor
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
//清空暂时变量
currentRegistryProcessors.clear();
// 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) {
//履行没有完成Ordered接口的BeanDefinitionRegistryPostProcessor
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.
// List registryProcessors
// 之前现已履行过BeanDefinitionRegistryPostProcessor独有办法,现在履行其父类办法
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
// List regularPostProcessors
// 履行 BeanFactoryPostProcessor 办法
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 的 beanName
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) {
// 假如现已被履行过了,就不在履行
// 因为一开端先获取的BeanDefinitionRegistryPostProcessor,而BeanDefinitionRegistryPostProcessor承继了BeanFactoryPostProcessor
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.
// 依据不同的优先级,按序履行 BeanFactoryPostProcessor
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
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.
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();
}

源码超级长,咱们渐渐来看。

  1. Spring容器运用的BeanFactory 是DefaultListableBeanFactory,它完成了BeanDefinitionRegistry接口,if条件建立。
  2. 优先处理程序传进来的beanFactoryPostProcessors,也便是咱们手动调用org.springframework.context.support.AbstractApplicationContext#addBeanFactoryPostProcessor办法来增加的BeanFactoryPostProcessor。
  3. BeanFactoryPostProcessor是一个尖端接口,他还有一个子类BeanDefinitionRegistryPostProcessor,声明两个List来寄存BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor,以便操控这两个接口办法的履行。
  4. 遍历传入的List beanFactoryPostProcessors,将其分类放到两个List中。假如传入的是BeanDefinitionRegistryPostProcessor类,则先履行BeanDefinitionRegistryPostProcessor类中独有的办法postProcessBeanDefinitionRegistry办法。当然,咱们这儿传入的List beanFactoryPostProcessors为空。
  5. 第一次履行beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);办法,从容器中获取BeanDefinitionRegistryPostProcessor类型的Bean的name(这儿仅仅获取称号,还没有实例化Bean)。留意,程序履行到这儿,Spring还没有扫描包,还没有将项目中的Bean注册到容器中。默许情况下,这儿回来的数据为:

回想一下,这个BeanDefinition是在什么时候被加入到BeanFactory的呢?是在AnnotationConfigApplicationContext的无参结构器中创立reader时注册的BeanDefinition。BeanName为org.springframework.context.annotation.internalConfigurationAnnotationProcessor,对应的Class为org.springframework.context.annotation.ConfigurationClassPostProcessor。

  1. 遍历这个获取的postProcessorNames,假如完成了PriorityOrdered接口,就调用beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)办法,从容器中获取这个Bean,将其加入到暂时变量List currentRegistryProcessors中。
  2. 对currentRegistryProcessors中的元素进行排序,然后履行BeanDefinitionRegistryPostProcessor中的特有办法postProcessBeanDefinitionRegistry。留意哦,这儿没有履行其父类的办法,而是又将其放到List registryProcessors中,到后边再履行其父类办法。
  3. 默许情况下,此刻currentRegistryProcessors中只要一个Bean即:org.springframework.context.annotation.ConfigurationClassPostProcessor(它完成了PriorityOrdered接口)。ConfigurationClassPostProcessor是一个非常重要的类,咱们后边在讲。当程序履行完ConfigurationClassPostProcessor的BeanDefinitionRegistryPostProcessor办法后,咱们程序中的Bean就被注册到了Spring容器中了,需求留意的是,这儿还仅仅注册了BeanDefinition,还没有创立Bean目标。

当第2次履行postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);办法,此刻因为之前现已完成了Bean的扫描,所以假如咱们有自定义的BeanDefinitionRegistryPostProcessor就能够在这儿被获取了。获取之前,判别其是否完成Ordered接口,而且之前没有被履行过,则调用getBean办法,从容器中获取该Bean,然后进行排序,履行postProcessBeanDefinitionRegistry办法。

前面现已按次序履行了完成PriorityOrdered 和Ordered接口的BeanDefinitionRegistryPostProcessor,最终,履Spring5源码剖析(四)refresh办法行没有完成Ordered接口的BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry办法。履行完之后再BeanDefinitionRegistryPostProcessor的父类办法postProcessBeanFactory。

获取容器中还没有被履行过的完成BeanFactoryPostProcessor接口的Bean,然后按次序履行的postProcessBeanFactory。默许情况下,这儿会获取到:

因为Bean org.springframework.context.annotation.internalConfigurationAnnotationProcessor(对应的Class为org.springframework.context.annotation.ConfigurationClassPostProcessor)在之前现已被履行了,这儿只会履行Bean org.springframework.context.event.internalEventListenerProcessor(对应的Class为org.springframework.context.event.EventListenerMethodProcessor)的postProcessBeanFactory办法,源码如下:

//org.springframework.context.event.EventListenerMethodProcessor#postProcessBeanFactory 源码
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
Map beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false);
List factories = new ArrayList<>(beans.values());
AnnotationAwareOrderComparator.sort(factories);
this.eventListenerFactories = factories;
}

记住点个赞再走哦~

为了感谢支撑我的朋友!整理了一份Java高档架构材料、Spring源码剖析、Dubbo、Redis、Netty、zookeeper、Spring cloud、分布式等材料

重视大众号:Java大型网站架构(免费获取)Spring5源码剖析(四)refresh办法