public class UserStatusListener implements ServletContextListener
{
/**
* Servlet容器启动的时候,将会执行此方法;
*/
public void contextInitialized(ServletContextEvent event)
{
//获取applicationContext文件所在的路径;
String path = event.getServletContext().getRealPath(File.separator)
+ event.getServletContext().getInitParameter("applicationContextConfigName");
// 把applicationContext.xml文件路径存在application作用域中;
event.getServletContext().setAttribute("applicationContextPath",path);
....
}
/**
* Servlet容器关闭时,则会执行此方法;
*/
public void contextDestroyed(ServletContextEvent arg0)
{
//其他操作
}
}
其中applicationContextConfigName是配置在WEB-INF/web.xml文件中
<!-- 监听时读取,Spring配置文件目录 -->
<context-param>
<param-name>applicationContextConfigName</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>