spring在web应用中的配置
2013-03-05 18:19
330 查看
Spring在Web应用中的配置
在WebApp中获得XMLWebApplicationContext的步骤1.在Web.xml中配置上下文载入器.
2.指定上下文载入器的配置文件.
3.获得应用上下文.
1.在Web.xml中配置上下文载入器
根据你的系统情况,你可以选择两种上下文载入器:ContextLoaderListener和ContextLoaderServlet.如果你的Web容器支持Servlet2.3标准或更高,你可以使用两者,否则只能使用后者.
ContextLoaderListener在Web.xml应该如下配置:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
ContextLoaderServlet在Web.xml应该如下配置:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
2.指定上下文载入器的配置文件
不论你使用的那种上下文载入器,你都应该指明Spring配置文件的位置.如果没有指定,上下文载入器将把/web-inf/application-Context.xml当作Spring配置文件。
要指定Spring配置文件的位置,你可以在Servlet上下文设置contextConfigLocation参数来为上下文载入器指定一个或多个Spring配置文件(使用通配符或是用逗号隔开)。如下所示:
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/cfg/bean.xml
</param-value>
</context-param>
3.获得应用上下文
接下来我们就可以获得ApplicationContext了,代码如下:
WebApplicationContext ctx
= WebApplicationContextUtils.getWebApplicationContext(servletContext);
在一个自启动的Servlet中,我们可以这样获得它:
public
class InitialSystemServlet
extends HttpServlet
{
public
void init(ServletConfig config)
throws ServletException
{
// 取得Spring的上下文
WebApplicationContext ctx
= WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
..
}
..
}
如果把获得的上下文的地址给一个静态引用,我们以后就可以在应用中的任意位置使用ApplicationContext了。
相关文章推荐
- 在Web应用中利用Spring配置log4j
- Spring中配置Hibernate事务管理器---开发线程安全的Spring Web应用 续
- java三大框架之spring手动配置web应用环境
- Spring基于注解的缓存配置--web应用实例
- 在weblogic下部署web应用,加载spring配置文件时要删除注释
- SPRINGMYBATIS01 Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
- Spring基于注解的缓存配置--web应用实例
- 深入理解Spring系列之七:web应用自动装配Spring配置
- Spring基于注解的缓存配置--web应用实例
- Spring在Web应用中的配置
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
- 缺省配置Springboot Web应用运行中DispatchServlet的初始化
- Spring基于注解的缓存配置--web应用实例
- Spring基于注解的缓存配置--web应用实例
- Spring基于注解的缓存配置--web应用实例
- 配置Spring WEB应用的方法
- Spring Web MVC简介 、基于XML配置的MVC应用 、基于注解配置的MVC应用
- Spring基于注解的缓存配置--web应用实例
- Spring Web 简单应用及配置
- Spring数据源的灵活配置巧应用