在Web应用程序中加载ApplicationContext
来源:互联网 发布:谈谈怎样看待网络暴力 编辑:程序博客网 时间:2024/06/08 18:27
在Web应用程序中,ApplicationContext是在web.xml文件中配置的。如果Servlet容器支持Servlet2.3规范,就可以使用org.springframework.web.context.ContextLoaderListener,如代码:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
ContextLoaderListener不能与某些Servlet2.3容器兼容。如果你使用的是这种Servlet容器,就需要在web.xml文件中配置org.springframework.web.context.ContextLoaderServlet Servlet,而不是ContextLoaderListener。ContextLoaderListener不能正确地与下面这些Servlet2.3容器一起工作。
- BEA Weblogic 8.1 SP2和更早的版本
- IBM WebSphere 6.0之前的版本
- Oracle OC4J 10g之前的版本
如果使用Servlet2.2容器,还必须使用ContextLoaderServlet,代码如下:
在web.xml中配置ContextLoaderServlet
<servlet>
<servlet-name>contextLoaderServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
由ContextLoaderListener和ContextLoaderServlet创建的ApplicationContext被放在Web应用程序的ServletContext对象中。如果提供ServletContext对象,org.springframework.web.context.support.WebApplicationContextUtils类就将返回ApplicationContext对象,像下面这样:
ApplicationContext applicationContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
默认情况下,ContextLoaderListener和ContextLoaderServlet加载/WEB-INF/applicationContext.xml文件。这个位置可以通过在web.xml中定义contextConfigLocation上下文参数进行覆盖,代码如下:
通过contextConfigLocation参数指定XML文件位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml <!--这里是你的配置文件-->
classpath:data-access-context.xml
</param-value>
</context-param>
由ContextLoaderListener和ContextLoaderServlet为应用程序上下文加载的默认资源位置是web应用程序的ServletContext对象。
以上配置好后,你就可以在你的Action当中利用ServletActionContext.getServletContext()来获取ServletContext对象。
- 在Web应用程序中加载ApplicationContext
- 初探spring applicationContext在web容器中加载过程
- 初探spring applicationContext在web容器中加载过程
- 初探spring applicationContext在web容器中加载过程
- web应用中加载applicationContext
- 初探spring applicationContext在web容器中加载过程 首先从WEB.XML入手
- 从源码看spring applicationContext在web容器中加载过程
- 从源码看spring applicationContext在web容器中加载过程
- 初探spring applicationContext在web容器中加载过程 首先从WEB.XML入手 ==>web.xml(1)
- WEB中加载ApplicationContext的两种方式
- 在web.xml中配置Servlet,Servlet被WEB应用程序加载过程
- 在web.xml中配置Servlet,Servlet被WEB应用程序加载过程
- 如何在Java Web应用中获取Spring的ApplicationContext
- Web项目中applicationContext.xml放在src下
- Spring中ApplicationContext加载机制
- Spring中ApplicationContext加载机制
- Spring中ApplicationContext加载机制
- Spring中ApplicationContext加载机制
- 安装sql server 2008,提示要删除SQL Server 2005 Express 工具的解决办法
- 约瑟夫环顺序输出方法及最终值计算
- 彻底搞定C语言指针 第四篇
- Glassfish性能调优 让你的Java EE更流畅
- Apache+php+mysql在windows下的安装与配置图解
- 在Web应用程序中加载ApplicationContext
- LoadModule php5_module/apache不能启动/加载失败/启动失败
- 程序是验证不出来的
- VC里辨别方向键
- 使用VS2005编译openssl
- Eclipse小技巧--快速输入System.out.println();
- 什么时候才能形成一种好的编程习惯?
- 【数据库】“表提示” 的概念
- js检测浏览器和操作系统-----摘录