欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

监听器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);
}

}