ContextLoaderListener載入過程(最詳細版)
以下描述,將是最詳細版spring的ContextLoaderListener載入過程,沒有之一。
ContextLoaderListener實現了ServletContextListener介面,ServletContextListener是Java EE標準介面之一,類似tomcat,jetty的java容器啟動時便會觸發該介面的contextInitialized。
1 顧,java容器啟動觸發ContextLoaderListener的contextInitialized
2 contextInitialized 方法呼叫ContextLoader的initWebApplicationContext方法。
3 initWebApplicationContext呼叫createWebApplicationContext方法
4 createWebApplicationContext 呼叫determineContextClass方法
5 determineContextClass有如下程式碼
contextClassName = defaultStrategies
.getProperty(WebApplicationContext.class.getName());
顯然是從defaultStrategies中載入的
ContextLoader 類中有段靜態程式碼
static {
try {
ClassPathResource resource = new ClassPathResource(
"ContextLoader.properties", ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException ex) {
throw new IllegalStateException(
"Could not load 'ContextLoader.properties': "
+ ex.getMessage());
}
currentContextPerThread = new ConcurrentHashMap(1);
}
ContextLoader.properties 檔案內容如下:
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
至此,determineContextClass方法返回的是XmlWebApplicationContext
6 回到 initWebApplicationContext 方法,呼叫configureAndRefreshWebApplicationContext方法
7 configureAndRefreshWebApplicationContext 呼叫了AbstractApplicationContext的refresh方法
8 refresh 方法呼叫了obtainFreshBeanFactory
9 obtainFreshBeanFactory 呼叫了AbstractRefreshableApplicationContext類的refreshBeanFactory方法
10 refreshBeanFactory呼叫了XmlWebApplicationContext的loadBeanDefinitions
11 loadBeanDefinitions中載入了對應的applicationContext.xml
如果你還沒看明白,請聯絡我。