监听器Listener中使用Spring创建的bean
程序员文章站
2022-05-01 19:18:01
...
package com.dxcollector.spider;
import java.util.Timer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* 任务监听器
*
* @author 忧里修斯
*
*/
public class TaskListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent context) {
}
public void contextInitialized(ServletContextEvent context) {
ServletContext servletContext = context.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
SheduleTask sheduleTask = (SheduleTask) wac.getBean("sheduleTask");
Timer timer = new Timer();
//每隔30分钟执行一次
// timer.schedule(new SheduleTask(), 0,1000*60*30);
timer.schedule(sheduleTask, 0,1000*60*30);
}
}
下一篇: Linux后台运行java-jar包