获得Controller 的Map ContextLoaderListener和ContextLoaderServlet
来源:互联网 发布:java源程序的扩展名是 编辑:程序博客网 时间:2024/04/30 08:04
Spring中ApplicationContext加载机制和配置初始化
引用:http://liuwei1578.blog.163.com/blog/static/495803642007116111923195/
Spring中ApplicationContext加载机制。
加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet。
这两者在功能上完全等同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,而另一个基于Servlet接口实现。开发中可根据目标Web容器的实际情况进行选择。
<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.
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 >
如果sturts-config.xml里配置了contextConfigLocation的值,那么就不会自动加载xx-servlet.xml了,而只会加载contextConfigLocation所指定的xml.
2. 第2种方式
在web.xml里配置Listener
- <listener>
- < listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- < / listener>
如果在web.xml里给该Listener指定要加载的xml,如:
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath*:spring/*.xmlparam-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启动要稍微延迟一些。
- 获得Controller 的Map ContextLoaderListener和ContextLoaderServlet
- spring学习之ApplicationContext—— ContextLoaderListener和ContextLoaderServlet
- spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
- spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
- 第九篇 sping深入学习之ContextLoaderListener与ContextLoaderServlet
- spring3.0以后移除ContextLoaderServlet,使用ContextLoaderListener获取ApplicationContext
- 获得rails的URL的controller和action
- 如何获得map的key和Value
- ContextLoaderListener的作用和配置
- ContextLoaderListener和DispatcherServlet的相互关系
- ServletContextListener和ContextLoaderListener的区别
- DispatcherServlet 和 ContextLoaderListener 的关系
- 非controller获得request和session
- SpringMVC-------->Controller获得上传文件的绝对路径
- SpringMVC-------->Controller获得上传文件的绝对路径
- Controller获得上传文件的绝对路径
- contextLoaderListener和dispatcherServlet初始化上下文的区别
- Spring中DispatcherServlet和ContextLoaderListener的区别
- 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
- 框架和平台的区别以及重要性
- 关于android的一些感想
- 正则习点 --- 08
- make clean、make mrproer 以及make distclean的区别
- 获得Controller 的Map ContextLoaderListener和ContextLoaderServlet
- 判断m-n之间有多少个素数,并输出所有素数。
- C++不定参数综合实例
- vim插件介绍
- 关于Android横竖屏切换的解决方法
- 配置基本
- 指向子类实例的父类指针 http://hi.baidu.com/qinlikang/blog/item/b6476bf52223972cbd310931.html
- 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
- 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。