启动Tomcat自动加载(运行)类

其实这是紧跟着我上次写的java计时器Timer的,因为Timer的测试类写好后,不可能要通过什么东西去触发,对已经存在的时间点进行监控

所以,在启动项目是自动运行此类

方法如下:

一、在web.xml中添加listener配置

<!-- 定时器监听 -->
< listener  >
     < listener-class >  com.xusk.system.core.initlistener.ProjectListener </ listener-class  >
</ listener >
二、创建类并继承 ServletContextListener
继承后会自动重写两个方法
public  class  ProjectListener  implements  ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
/**
* 启动tomcat后会运行此方法
*/
@Override
public void contextInitialized(ServletContextEvent arg0) {
}
}
原文地址:https://www.cnblogs.com/snake-hand/p/3165682.html