参考:http://blog.csdn.net/wxwzy738/article/details/16983935
http://www.cnblogs.com/hzj-/articles/1689836.html
<context-param>的作用:
web.xml的配置中<context-param>配置作用
说到<content-param> 就不得不说下 classpath
classpath:指WEB-INF 下的class目录,指的是web容器在加载<context-param>的时候,将加载classpath指定的文件到上下文中
classpath*: 不仅指WEB-INF 下的class目录,还包括各jar下的class路径下查找
如下配制:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-mvc.xml</param-value>
</context-param>
会去这个下面找对应的文件
classpath还支持通配符的格式,如下配制:
<!-- 加载所有的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:**/spring-*.xml</param-value>
</context-param>
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <context-param></context-param>和 <listener></listener>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
在这个方法中获得ServletContext = ServletContextEvent.getServletContext(),然后将context设置到spring的上下文中
6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.
7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.
8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.