ServletContextListener
该接口是Servlet包中的接口,在Tomcat启动时会执行该接口对象的contextInitialized(ServletContextEvent sce)方法,当Tomcat关闭时会调用该接口对象的contextDestroyed(ServletContextEvent sce)方法。
web.xml配置
<!-- web.xml 文件 -->
<web-app>
<listener>
<!---监听 --->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
// Create a new {@code ContextLoaderListener} with the given application context. This constructor is useful in Servlet 3.0+ environments where instance-based
public ContextLoaderListener(WebApplicationContext context) {
super(context);
}
//Initialize the root web application context.
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
// Close the root web application context.
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
该接口相当于Tomcat启动和关闭事件的回调接口,如果想在Tomcat启动或者关闭做一些事情