码迷,mamicode.com
首页 > 移动开发 > 详细

Spring源码学习(六)AbstractApplicationContext.refresh()

时间:2020-01-09 01:07:13      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:ready   created   his   添加   depend   todo   list   记录   remove   

加油加油 ??

 

大致过程

 1     @Override
 2     public void refresh() throws BeansException, IllegalStateException {
 3         synchronized (this.startupShutdownMonitor) {
 4             // Prepare this context for refreshing.
 5             // 准备刷新的上下文环境
 6             prepareRefresh();
 7 
 8             // Tell the subclass to refresh the internal bean factory.
 9             //并读取XML,解析注册beanDefination,初始化beanFactory
10             //
11             ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
12 
13             // Prepare the bean factory for use in this context.
14             //对beanFactory进行功能填充
15             prepareBeanFactory(beanFactory);
16 
17             try {
18                 // Allows post-processing of the bean factory in context subclasses.
19                 //
20                 postProcessBeanFactory(beanFactory);
21 
22                 // Invoke factory processors registered as beans in the context.
23                 // 激活各种beanFactory的处理器
24                 invokeBeanFactoryPostProcessors(beanFactory);
25 
26                 // Register bean processors that intercept bean creation.
27                 // 注册bean创建的Bean处理器(仅注册)
28                 registerBeanPostProcessors(beanFactory);
29 
30                 // Initialize message source for this context.
31                 //为上下文初始化message,即不同语言的消息体,国际化处理
32                 initMessageSource();
33 
34                 // Initialize event multicaster for this context.
35                 // 初始化应用消息广播器,
36                 initApplicationEventMulticaster();
37 
38                 // Initialize other special beans in specific context subclasses.
39                 // 留给子类扩展
40                 onRefresh();
41 
42                 // Check for listener beans and register them.
43                 // 在所有注册的bean中查找Listener bean,注册到消息广播器中
44                 registerListeners();
45 
46                 // Instantiate all remaining (non-lazy-init) singletons.
47                 // 初始化剩下的单实例
48                 finishBeanFactoryInitialization(beanFactory);
49 
50                 // Last step: publish corresponding event.
51                 // 通知生命周期处理器刷新过程,同时发出ContextRefreshedEvent
52                 finishRefresh();
53             }
54 
55             catch (BeansException ex) {
56                 if (logger.isWarnEnabled()) {
57                     logger.warn("Exception encountered during context initialization - " +
58                             "cancelling refresh attempt: " + ex);
59                 }
60 
61                 // Destroy already created singletons to avoid dangling resources.
62                 destroyBeans();
63 
64                 // Reset ‘active‘ flag.
65                 cancelRefresh(ex);
66 
67                 // Propagate exception to caller.
68                 throw ex;
69             }
70 
71             finally {
72                 // Reset common introspection caches in Spring‘s core, since we
73                 // might not ever need metadata for singleton beans anymore...
74                 resetCommonCaches();
75             }
76         }
77     }

 

(一)环境准备

 1     protected void prepareRefresh() {
 2         // Switch to active.
 3         this.startupDate = System.currentTimeMillis();
 4         this.closed.set(false);
 5         this.active.set(true);
 6 
 7         if (logger.isDebugEnabled()) {
 8             if (logger.isTraceEnabled()) {
 9                 logger.trace("Refreshing " + this);
10             }
11             else {
12                 logger.debug("Refreshing " + getDisplayName());
13             }
14         }
15 
16         // Initialize any placeholder property sources in the context environment.
17         // 空方法 留给子类扩展使用
18         // 继承ClassPathXmlApplicationContext重写initPropertySources()方法即可添加定制化验证
19         initPropertySources();
20 
21         // Validate that all properties marked as required are resolvable:
22         // see ConfigurablePropertyResolver#setRequiredProperties
23         //检验属性的合法
24         getEnvironment().validateRequiredProperties();
25 
26         // Store pre-refresh ApplicationListeners...
27         if (this.earlyApplicationListeners == null) {
28             this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
29         }
30         else {
31             // Reset local application listeners to pre-refresh state.
32             this.applicationListeners.clear();
33             this.applicationListeners.addAll(this.earlyApplicationListeners);
34         }
35 
36         // Allow for the collection of early ApplicationEvents,
37         // to be published once the multicaster is available...
38         this.earlyApplicationEvents = new LinkedHashSet<>();
39     }
(1)留给子类扩展接口
(2)验证属性合法性
(3)记录容器中的早期事件
(二)加载beanFactory
1     protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
2         refreshBeanFactory();
3         return getBeanFactory();
4     }
 1     @Override
 2     protected final void refreshBeanFactory() throws BeansException {
 3         if (hasBeanFactory()) {
 4             destroyBeans();
 5             closeBeanFactory();
 6         }
 7         try {
 8             //(1)为上下文创建DefaultListableBeanFactory,并设置序列化id
 9             DefaultListableBeanFactory beanFactory = createBeanFactory();
10             beanFactory.setSerializationId(getId());
11             //(2)customizeBeanFactory()配置覆盖方法,循环依赖,子类可覆盖,是一个扩展接口,同样继承ClassPathXmlApplicationContext重写此方法即可
12             customizeBeanFactory(beanFactory); // 配置覆盖方法,循环依赖,子类可覆盖
13             loadBeanDefinitions(beanFactory);// (1)创建XmlBeanDefinitionReader,(2)initBeanDefinitionReader()可扩展接口(3)loadBeanDefinitions:解析xml为beanDefinition
14             synchronized (this.beanFactoryMonitor) {
15                 this.beanFactory = beanFactory;
16             }
17         }
18         catch (IOException ex) {
19             throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
20         }
21     }

   最重要的就是解析beanDefinition,注册到beanDefinitionMap中

(3)prepareBeanFactory()功能扩展
 1     protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
 2         // Tell the internal bean factory to use the context‘s class loader etc.
 3         beanFactory.setBeanClassLoader(getClassLoader());
 4         // TODO SpEl处理器如何使用
 5         beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
 6         // TODO 属性注册编辑器
 7         beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
 8 
 9         // Configure the bean factory with context callbacks.
10         // 增加内置类
11         beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
12         // 设置忽略自动装配的接口
13         beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
14         beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
15         beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
16         beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
17         beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
18         beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
19 
20         // BeanFactory interface not registered as resolvable type in a plain factory.
21         // MessageSource registered (and found for autowiring) as a bean.
22         // 设置自动装配的特殊规则
23         beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
24         beanFactory.registerResolvableDependency(ResourceLoader.class, this);
25         beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
26         beanFactory.registerResolvableDependency(ApplicationContext.class, this);
27 
28         // Register early post-processor for detecting inner beans as ApplicationListeners.
29         beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
30 
31         // Detect a LoadTimeWeaver and prepare for weaving, if found.
32         // 增加对AspectJ的支持
33         if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
34             beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
35             // Set a temporary ClassLoader for type matching.
36             beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
37         }
38 
39         // Register default environment beans.
40         if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
41             beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
42         }
43         if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
44             beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
45         }
46         if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
47             beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
48         }
49     }

(四)postProcessBeanFactory()是一个可扩展接口

(五)invokeBeanFactoryPostProcessors

 

 1     protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
 2         PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
 3 
 4         // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
 5         // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
 6         if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
 7             beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
 8             beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
 9         }
10     }
  1     public static void invokeBeanFactoryPostProcessors(
  2             ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
  3 
  4         // Invoke BeanDefinitionRegistryPostProcessors first, if any.
  5         Set<String> processedBeans = new HashSet<>();
  6 
  7         if (beanFactory instanceof BeanDefinitionRegistry) {
  8             BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
  9             List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
 10             List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
 11 
 12             // (1)beanFactory是BeanDefinitionRegistry类型,postProcessor是BeanDefinitionRegistryPostProcessor类型
 13             //       postProcessor转化为BeanDefinitionRegistryPostProcessor类型添加进 registryProcessors
 14             // (2)beanFactory是BeanDefinitionRegistry类型,但postProcessor不是BeanDefinitionRegistryPostProcessor类型
 15             //      postProcessor添加进 regularPostProcessors
 16             for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
 17                 if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
 18                     BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor;
 19                     //TODO 调用了?
 20                     registryProcessor.postProcessBeanDefinitionRegistry(registry);
 21 
 22                     registryProcessors.add(registryProcessor);
 23                 } else {
 24                     regularPostProcessors.add(postProcessor);
 25                 }
 26             }
 27 
 28             // Do not initialize FactoryBeans here: We need to leave all regular beans
 29             // uninitialized to let the bean factory post-processors apply to them!
 30             // Separate between BeanDefinitionRegistryPostProcessors that implement
 31             // PriorityOrdered, Ordered, and the rest.
 32             List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
 33 
 34             // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
 35             // BeanDefinitionRegistryPostProcessors+PriorityOrdered→invokeBeanDefinitionRegistryPostProcessors()
 36             String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 37             for (String ppName : postProcessorNames) {
 38                 if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
 39                     currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 40                     processedBeans.add(ppName);
 41                 }
 42             }
 43             sortPostProcessors(currentRegistryProcessors, beanFactory);
 44             registryProcessors.addAll(currentRegistryProcessors);
 45             invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 46             currentRegistryProcessors.clear();
 47 
 48             // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
 49             // BeanDefinitionRegistryPostProcessors+Ordered→invokeBeanDefinitionRegistryPostProcessors()
 50             postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 51             for (String ppName : postProcessorNames) {
 52                 if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
 53                     currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 54                     processedBeans.add(ppName);
 55                 }
 56             }
 57             sortPostProcessors(currentRegistryProcessors, beanFactory);
 58             registryProcessors.addAll(currentRegistryProcessors);
 59             invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 60             currentRegistryProcessors.clear();
 61 
 62             // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
 63             // BeanDefinitionRegistryPostProcessors→invokeBeanDefinitionRegistryPostProcessors()
 64             boolean reiterate = true;
 65             while (reiterate) {
 66                 reiterate = false;
 67                 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 68                 for (String ppName : postProcessorNames) {
 69                     // 避免重复加载
 70                     if (!processedBeans.contains(ppName)) {
 71                         currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 72                         processedBeans.add(ppName);
 73                         reiterate = true;
 74                     }
 75                 }
 76                 sortPostProcessors(currentRegistryProcessors, beanFactory);
 77                 registryProcessors.addAll(currentRegistryProcessors);
 78                 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 79                 currentRegistryProcessors.clear();
 80             }
 81 
 82             // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
 83             //  BeanDefinitionRegistryPostProcessor
 84             invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
 85             //  BeanFactoryPostProcessor
 86             invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
 87         } else {
 88             // Invoke factory processors registered with the context instance.
 89             invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
 90         }
 91 
 92         // Do not initialize FactoryBeans here: We need to leave all regular beans
 93         // uninitialized to let the bean factory post-processors apply to them!
 94         String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
 95 
 96         // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
 97         // Ordered, and the rest.
 98         List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
 99         List<String> orderedPostProcessorNames = new ArrayList<>();
100         List<String> nonOrderedPostProcessorNames = new ArrayList<>();
101         for (String ppName : postProcessorNames) {
102             if (processedBeans.contains(ppName)) {
103                 // 避免重复加载
104                 // skip - already processed in first phase above
105             } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
106                 priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
107             } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
108                 orderedPostProcessorNames.add(ppName);
109             } else {
110                 nonOrderedPostProcessorNames.add(ppName);
111             }
112         }
113 
114         // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
115         // BeanFactoryPostProcessor+PriorityOrdered→invokeBeanFactoryPostProcessors()
116         sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
117         invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
118 
119         // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
120         //BeanFactoryPostProcessor+Ordered→invokeBeanFactoryPostProcessors()
121         List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
122         for (String postProcessorName : orderedPostProcessorNames) {
123             orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
124         }
125         sortPostProcessors(orderedPostProcessors, beanFactory);
126         invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
127 
128         // Finally, invoke all other BeanFactoryPostProcessors.
129         //BeanFactoryPostProcessors→invokeBeanFactoryPostProcessors()
130         List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
131         for (String postProcessorName : nonOrderedPostProcessorNames) {
132             nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
133         }
134         invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
135 
136         // Clear cached merged bean definitions since the post-processors might have
137         // modified the original metadata, e.g. replacing placeholders in values...
138         beanFactory.clearMetadataCache();
139     }
(1)了解BeanDefinitionRegistryPostProcessor 实现了 BeanFactoryPostProcessor
1 public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
2 
3     void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
(2)了解BeanFactoryPostProcessor
1 public interface BeanFactoryPostProcessor {
2 
3    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
4 
5 }

(3)了解PriorityOrdered实现Ordered

 

1 public interface PriorityOrdered extends Ordered {
2 }

(4)了解Ordered

 1 public interface Ordered {
 2 
 3     /**
 4      * Useful constant for the highest precedence value.
 5      * @see java.lang.Integer#MIN_VALUE
 6      */
 7     int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
 8 
 9     /**
10      * Useful constant for the lowest precedence value.
11      * @see java.lang.Integer#MAX_VALUE
12      */
13     int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
14 
15 
16     int getOrder();
17 
18 }

加载优先级:

(a)BeanDefinitionRegistryPostProcessor优先于BeanFactoryPostProcessor
(b)PriorityOrdered优先于Ordered,其中order值越小越先加载

(六)注册
BeanPostProcessor
1     protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
2         PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
3     }

 1 public static void registerBeanPostProcessors(
 2             ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
 3 
 4         String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
 5 
 6         // Register BeanPostProcessorChecker that logs an info message when
 7         // a bean is created during BeanPostProcessor instantiation, i.e. when
 8         // a bean is not eligible for getting processed by all BeanPostProcessors.
 9         //当后置处理器还没有注册就已经开始bean初始化时打印出BeanPostProcessorChecker的信息
10         int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
11         beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
12 
13         // Separate between BeanPostProcessors that implement PriorityOrdered,
14         // Ordered, and the rest.
15         //根据是否继承PriorityOrdered,Ordered, and the rest分类
16         List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
17         List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
18         List<String> orderedPostProcessorNames = new ArrayList<>();
19         List<String> nonOrderedPostProcessorNames = new ArrayList<>();
20         for (String ppName : postProcessorNames) {
21             if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
22                 BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
23                 priorityOrderedPostProcessors.add(pp);
24                 if (pp instanceof MergedBeanDefinitionPostProcessor) {
25                     internalPostProcessors.add(pp);
26                 }
27             } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
28                 orderedPostProcessorNames.add(ppName);
29             } else {
30                 nonOrderedPostProcessorNames.add(ppName);
31             }
32         }
33 
34         // First, register the BeanPostProcessors that implement PriorityOrdered.
35         sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
36         // 注册实现PriorityOrdered.的
37         registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
38 
39         // Next, register the BeanPostProcessors that implement Ordered.
40         List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
41         for (String ppName : orderedPostProcessorNames) {
42             BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
43             orderedPostProcessors.add(pp);
44             if (pp instanceof MergedBeanDefinitionPostProcessor) {
45                 internalPostProcessors.add(pp);
46             }
47         }
48         //排序
49         sortPostProcessors(orderedPostProcessors, beanFactory);
50         //注册实现 Ordered 的
51         registerBeanPostProcessors(beanFactory, orderedPostProcessors);
52 
53         // Now, register all regular BeanPostProcessors.
54         List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
55         for (String ppName : nonOrderedPostProcessorNames) {
56             BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
57             nonOrderedPostProcessors.add(pp);
58             if (pp instanceof MergedBeanDefinitionPostProcessor) {
59                 internalPostProcessors.add(pp);
60             }
61         }
62         // 注册实现没有实现PriorityOrderedOrdered和的
63         registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
64 
65         // Finally, re-register all internal BeanPostProcessors.
66         sortPostProcessors(internalPostProcessors, beanFactory);
67         // 注册所有MergedBeanDefinitionPostProcessor类型的PostProcessors
68         registerBeanPostProcessors(beanFactory, internalPostProcessors);
69 
70         // Re-register post-processor for detecting inner beans as ApplicationListeners,
71         // moving it to the end of the processor chain (for picking up proxies etc).
72         // 注册ApplicationListenerDetector
73         beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
74     }

 

 1     @Override
 2     public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
 3         Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
 4         // Remove from old position, if any
 5         this.beanPostProcessors.remove(beanPostProcessor);
 6         // Track whether it is instantiation/destruction aware
 7         if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
 8             this.hasInstantiationAwareBeanPostProcessors = true;
 9         }
10         if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
11             this.hasDestructionAwareBeanPostProcessors = true;
12         }
13         // 先删后增 保证是唯一最新
14         // Add to end of list
15         this.beanPostProcessors.add(beanPostProcessor);
16     }

(1) 顺序:BeanPostProcessorChecker>PriorityOrdered>Ordered>无序>MergedBeanDefinitionPostProcessor类型>ApplicationListenerDetector

(2) addBeanPostProcessor 先删后增 保证是唯一最新

(七) 初始化消息资源

 1     protected void initMessageSource() {
 2         ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 3         // bean id = messageSource
 4         if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
 5             this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
 6             // Make MessageSource aware of parent MessageSource.
 7             if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
 8                 HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
 9                 if (hms.getParentMessageSource() == null) {
10                     // Only set parent context as parent MessageSource if no parent MessageSource
11                     // registered already.
12                     hms.setParentMessageSource(getInternalParentMessageSource());
13                 }
14             }
15             if (logger.isTraceEnabled()) {
16                 logger.trace("Using MessageSource [" + this.messageSource + "]");
17             }
18         }
19         else {
20             // Use empty MessageSource to be able to accept getMessage calls.
21             DelegatingMessageSource dms = new DelegatingMessageSource();
22             dms.setParentMessageSource(getInternalParentMessageSource());
23             this.messageSource = dms;
24             beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
25             if (logger.isTraceEnabled()) {
26                 logger.trace("No ‘" + MESSAGE_SOURCE_BEAN_NAME + "‘ bean, using [" + this.messageSource + "]");
27             }
28         }
29     }

 

 

 

加油??  

 






Spring源码学习(六)AbstractApplicationContext.refresh()

标签:ready   created   his   添加   depend   todo   list   记录   remove   

原文地址:https://www.cnblogs.com/VVII/p/12168784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!