Spring中ApplicationContext加載機制。
加載器目前有兩種選擇:ContextLoaderListener和ContextLoaderServlet。
這兩者在功能上完全等同,只是一個是基於Servlet2.3版本中新引入的Listener接口實現,而另一個基於Servlet接口實現。開發中可根據目標Web容器的實際情況進行選擇。
配置非常簡單,在web.xml中增加:
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
或:
<servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
通過以上配置,Web容器會自動加載/WEB-INF/applicationContext.xml初始化
ApplicationContext實例,如果需要指定配置文件位置,可通過context-param加以指定:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/myApplicationContext.xml</param-value> </context-param>
配置完成之后,即可通過
WebApplicationContextUtils.getWebApplicationContext方法在Web應用中獲取ApplicationContext引用。
如:
-------------------------------------------------------------------------------------------
spring為ApplicationContext提供有三種實現(舉例)
1. FileSystemXmlApplicationContext
//eg1. ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加載單個配置文件 //eg2. String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"}; ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //加載多個配置文件 //eg3. ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");//根據具體路徑加載文件
3. XmlWebApplicationContext
eg1. ServletContext servletContext = request.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); 注 : 一般是 ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
1. 在struts-config.xml里,以插件的形式
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn" /> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> </plug-in >
這種方式如果沒有配置contextConfigLocation的值,則會自動加載xx-servlet.xml.xx的值是和web.xml里的配置org.apache.struts.action.ActionServlet的servlet-name的值一樣如下:xx的值也就是 action,所以會自動加載action-servlet.xml
<servlet> <servlet-name>action</servlet-name > <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name >action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> </listener>
<context-param> <param-name>contextConfigLocation<param-name> <param-value>classpath*:spring/*.xml<param-value> <context-param>
則會去加載相應的xml,而不會去加載/WEB-INF/下的applicationContext.xml。。但是,如果沒有指定的話,默認會去/WEB-INF/下加載applicationContext.xml。
3. 第三種方式:ContextLoaderServlet
<servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
這種方式和第二種Listener方式一樣,唯一的區別就是用Listener方式初始化ApplicationContext,可以和用第一種方式(struts-config.xml里 plugin方式)同時存在,而ContextLoaderServlet則不可以和第一種方式同時存在
總結:
ContextLoaderServlet已經不推薦用了,它只是為了兼容低版本的servlet.jar才用的。
總的來說:Listerner要比Servlet更好一些,而且Listerner監聽應用的啟動和結束,而Servlet啟動要稍微延遲一些。