提交 17552d20 编写于 作者: C congshuo_cnki

refresh

上级 75bcfb82
......@@ -40,6 +40,10 @@ public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProc
*
* @param registry the bean definition registry used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*
* 在标准初始化之后修改应用程序上下文的内部 bean 定义注册表。所有常规 bean 定义都将被加载,
* 但尚未实例化任何 bean。这允许在下一个后处理阶段开始之前添加更多的 bean 定义。
* 参数:registry - 应用程序上下文使用的 bean 定义注册
*/
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
......
package org.springframework.beans.cstest.jvm;
public class DemoOne {
public static void main(String[] args) {
String s = new String("1111");
String intern = s.intern();
System.out.println(intern);
String ss = new String("1") + new String("1");
String sss = "22";
ss.intern();
System.err.println(ss == sss);
}
}
......@@ -198,7 +198,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
@Nullable
private ConfigurableEnvironment environment;
/** BeanFactoryPostProcessors to apply on refresh. */
/** BeanFactoryPostProcessors to apply on refresh. 在刷新是使用*/
private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors = new ArrayList<>();
/** System time in milliseconds when this context started. */
......
......@@ -80,17 +80,23 @@ final class PostProcessorRegistrationDelegate {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>();
/** 对BeanDefinitionRegistry类型的处理*/
if (beanFactory instanceof BeanDefinitionRegistry registry) {
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
/** BeanDefinitionRegistryPostProcessor */
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor registryProcessor) {
/**
* 对于BeanDefinitionRegistryPostProcessor类型,在BeanFactoryPostProcessor的基础上还有
* 自己定义的方法,需要先调用
*/
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
/** 记录常规 BeanFactoryPostProcessor*/
regularPostProcessors.add(postProcessor);
}
}
......@@ -99,9 +105,17 @@ final class PostProcessorRegistrationDelegate {
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
/**
* 不要在此处初始化 FactoryBeans:我们需要让所有常规 bean 保持未初始化状态,
* 以便 bean 工厂后处理器应用到它们!将实现 PriorityOrdered、Ordered 和其余部分的
* BeanDefinitionRegistryPostProcessor 分开。
*/
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
/**
* 首先,调用实现 PriorityOrdered 的 BeanDefinitionRegistryPostProcessor。
*/
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
......@@ -226,14 +240,23 @@ final class PostProcessorRegistrationDelegate {
// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
/**
* BeanPostProcessorChecker 是一个普通的信息打印,可能会有些情况,
* 当Spring的配置中的后处理器还没有被注册就已经开始了bean的初始化时
* 便会打印出BeanPostProcessorChecker中设定的信息。
*/
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
/** 调用priorityOrdered保证排序*/
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
/** MergedBeanDefinitionPostProcessor*/
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
/** 使用Ordered 保证排序*/
List<String> orderedPostProcessorNames = new ArrayList<>();
/** 无序BeanPostProcessor*/
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
......@@ -252,9 +275,11 @@ final class PostProcessorRegistrationDelegate {
}
// First, register the BeanPostProcessors that implement PriorityOrdered.
/** 第 1 步,注册实现 PriorityOrdered 的 BeanPostProcessor。*/
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
/** 第 2 步,注册所有实现Ordered的BeanPostProcessor*/
// Next, register the BeanPostProcessors that implement Ordered.
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String ppName : orderedPostProcessorNames) {
......@@ -267,6 +292,7 @@ final class PostProcessorRegistrationDelegate {
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
/** 第 3 步,注册所有无序的BeanPostProcessor*/
// Now, register all regular BeanPostProcessors.
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String ppName : nonOrderedPostProcessorNames) {
......@@ -278,12 +304,19 @@ final class PostProcessorRegistrationDelegate {
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
/**
* 第 4 步,注册所有MergedBeanDefinitionPostProcessor类型的BeanPostProcessor,
* 并非重复注册
*
* 在beanFactory.addBeanPostProcessor中会先移出已存在的BeanPostProcessor
*/
// Finally, re-register all internal BeanPostProcessors.
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
/** 添加 ApplicationListener探测器*/
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册