Java ServletContextListener在Tomcat7中初始化并在中间销毁

Java ServletContextListener在Tomcat7中初始化并在中间销毁,java,tomcat,Java,Tomcat,我在Tomcat6环境中开发了一个web应用程序。此应用程序实现了一个ServletContextListener,如下所示: public class AppListener implements ServletContextListener { private static final Log log = LogFactory.getLog("STLOG." + AppListener.class.getName()); public void contextDestroyed(S

我在Tomcat6环境中开发了一个web应用程序。此应用程序实现了一个ServletContextListener,如下所示:

public  class AppListener implements ServletContextListener {
  private static final Log log = LogFactory.getLog("STLOG." + AppListener.class.getName());

 public void contextDestroyed(ServletContextEvent event){
  log.info("MyWebApp is finished.");
 }
 public void contextInitialized(ServletContextEvent event){
  log.info("MyWebApp is ready.");
 }
}
当MyWebApp在Tomcat 6中运行时,日志显示MyWebApp已准备就绪。当服务器启动并且MyWebApp完成时。当它关闭时。 但有意思的是,当应用程序在Tomcat7服务器上运行时,我会在启动时收到这两条消息。 在其他方面,我注意到,该应用程序在两台服务器上的工作原理相同,这很好。 任何人都可以告诉我为什么会有这种不同,以及它在我的应用程序生命周期中可能产生的影响?

您在Tomcat7部署上尝试过mywebapp/WEB-INF/WEB.xml give version=3.0属性吗

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0" >

  <listener>
    <listener-class>com.myapp.AppListener</listener-class>
  </listener>

...rest, listeners, filters, servlets...

</web-app>

也许你的Tomcat7只是测试webapp是否有效,然后卸载它,直到你真的向它发出任何请求?检查是否再次调用contextInitialized,如果将其加载到浏览器中,则不会调用contextDestroyed。