这里有新鲜出炉的精品教程,程序狗速度看过来!
Spring 是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于 IOC 和 AOP 的构架多层 j2ee 系统的框架,但它不强迫你必须在每一层 中必须使用 Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的 MVC,对不同的数据访问技术提供了统一的接口,采用 IOC 使得可以很容易的实现 bean 的装配,提供了简洁的 AOP 并据此实现 Transcation Managment,等等
这篇文章主要介绍了 Spring 在 web.xml 中的配置详细介绍的相关资料, 需要的朋友可以参考下
Spring 在 web.xml 中的配置详细介绍
前言
在实际项目中 spring 的配置文件 applicationcontext.xml 是通过 spring 提供的加载机制自动加载到容器中。在 web 项目中,配置文件加载到 web 容器中进行解析。目前,spring 提供了两种加载器,以供 web 容器的加载:一种是 ContextLoaderListener,另一种是 ContextLoaderServlet。这两种在功能上完全相同,只是前一种是基于 Servlet2.3 版本中新引入的 Listener 接口实现,而后一种是基于 Servlet 接口实现,以下是这两种加载器在 web.xml 中的配置应用:
ContextLoaderListener
- <listener>
- <listener-class>org.springframework.context.ContextLoaderListener</listener-class>
- </listener>
ContextLoaderServlet
- <servlet>
- <servlet-name>context</servlet-name>
- <servlet-class>org.springframework.context.ContextLoaderServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
通过上面的配置,web 容器会自动加载 applicationcontext.xml 初始化。
如果需要指定配置文件的位置,可通过 context-param 加以指定:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/myApplicationContext.xml</param-value>
- </context-param>
之后,可以通过 WebApplicationContextUtils.getWebApplicationContext 方法在 web 应用中获取 applicationcontext 的引用。
来源: http://www.phperz.com/article/17/0821/338972.html