spring 研究裝載配置文件
1,研究spring webApplicationContext初始化。spring 如何初始化。
spring 有兩種方法,一個(gè)是ContextLoaderListener這個(gè)Listerner,另一個(gè)是ContextLoaderServlet這個(gè)Servlet,這兩個(gè)方法都是在web應(yīng)用啟動(dòng)的時(shí)候來初始化WebApplicationContext,ContextLoader是一個(gè)工具類,用來初始化WebApplicationContext,其主要方法就是initWebApplicationContext,ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默認(rèn)實(shí)現(xiàn)類)放在了ServletContext中,ServletContext也是一個(gè)“容器”,也是一個(gè)類似Map的結(jié)構(gòu),而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我們?nèi)绻褂肳ebApplicationContext則需要從ServletContext取出,Spring提供了一WebApplicationContextUtils類,可以方便的取出WebApplicationContext,只要把ServletContext傳入就可以了。
2,研究通過使用ApplicationContext對spring 進(jìn)行初始化。
初始化流程,在初始化的時(shí)候,新增一個(gè)加載servlet ,這個(gè)serlvet負(fù)責(zé)初始化。AppContextLoader 提供了初始化applicationContext的方法,
通過定義ClassPathXmlApplicationContext 來加載spring 配置文件。裝載配置文件。
并把裝載的ApplicationContext文件放在servletContext中作為全局變量來使用。在使用ClassPathXmlApplicationContext 則需要從ServletContext取出.