加载器目前有两种选择: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引用。
如:
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(); LoginAction action=(LoginAction)ctx.getBean("action");
-------------------------------------------------------------------------------------------
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");//根据具体路径加载文件
2. ClassPathXmlApplicationContext
//eg1. ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); //eg2. String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"}; ApplicationContext ctx = new ClassPathXmlApplication(locations);注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的。
3. XmlWebApplicationContext
eg1. ServletContext servletContext = request.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
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>如果在web.xml里给该Listener指定要加载的xml,如:
<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启动要稍微延迟一些。
相关推荐
Spring中ApplicationContext加载机制
Spring中ApplicationContext和beanfactory区别.rar
Spring中ApplicationContext对事件传递
主要介绍了 Spring获取ApplicationContext对象工具类的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
spring的Applicationcontext对事件的监听,在一个applicationContext范围内,实现类似MQ的效果
spring配置对象实例化后执行的方法,两种实现比对,通过InitializingBean接口,并实现arterPropertiesSet()方法;或者通过applicationContext.xml配置init-method属性
spring 获得applicationcontext公用方法
Spring配置文件ApplicationContext,内容齐全,有需要的可以下载。
spring2.5的applicationContext配置文件.
applicationContext.xml 详细配置
Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext
applicationContext.xml+hiberniate属性+Session属性+自动代理+拦截器!!!! 上传的限制去除了jar包!
《Spring的数据源配置文件模板》applicationContext.zip
applicationContext.xml完美配置
本篇文章主要介绍了详解spring applicationContext.xml 配置文件 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
<description>Spring公共配置文件 <!-- mes 的數據庫 --> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> ...
NULL 博文链接:https://mutourenoo.iteye.com/blog/1156865
spring+jpa的applicationContext.xml配置
NULL 博文链接:https://ylxy3058.iteye.com/blog/2223687
NULL 博文链接:https://canfly2010.iteye.com/blog/443063