实现在项目启动时加载数据或者做一些初始化操作
第一步:自定义监听实现 ServletContextListener
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println(">>>>>>>>>> INIT <<<<<<<<<<");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println(">>>>>>>>>> DESTROYED <<<<<<<<<<");
}
}
第二步:在web.xml 注册监听(全路径)
<listener>
<listener-class>com.zxx.until.MyListener</listener-class>
</listener>