学习记录一下applicationContext在初始化过程中做的一些操作..

从全局整体来看.不会涉及太多细节.

Spring ApplicationContext 初始化流程

 

applicationcontext的refresh方法是一个startup method.算是spring启动的一个方法.它处理各种配置,不管是XML还是properties还是啥.创建了各种bean.算是一个最核心的方法.

refresh方法里面包含了一大堆模板方法.每个模板都做了一些事情,相当于一个小步骤..

从代码整体来看大概包含了这么一些步骤.其中2,3,4,5,6这几部是我个人觉得最核心的步骤.我想特别记录下我的想法

 

1. prepareRefresh

 1     /**
 2      * Prepare this context for refreshing, setting its startup date and
 3      * active flag as well as performing any initialization of property sources.
 4      */
 5     protected void prepareRefresh() {
 6         this.startupDate = System.currentTimeMillis();
 7         this.active.set(true);
 8 
 9         if (logger.isInfoEnabled()) {
10             logger.info("Refreshing " + this);
11         }
12 
13         // Initialize any placeholder property sources in the context environment
14         initPropertySources();
15 
16         // Validate that all properties marked as required are resolvable
17         // see ConfigurablePropertyResolver#setRequiredProperties
18         getEnvironment().validateRequiredProperties();
19 
20         // Allow for the collection of early ApplicationEvents,
21         // to be published once the multicaster is available...
22         this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
23     }
View Code

相关文章: