Listener中如何注入Service
public class ClearTimerListener implements ServletContextListener
{
private Timer timer = null;
private MessageInfoService messageInfoService;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
messageInfoService = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext())
.getBean(MessageInfoServiceImpl.class);
timer = new Timer(true);
servletContextEvent.getServletContext().log("刪除訊息記錄定時器已啟動");
timer.scheduleAtFixedRate(new ClearMessageTask(messageInfoService), 60 * 1000, 24 * 60 * 60 * 1000);
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
if(timer != null)
{
timer.cancel();
sce.getServletContext().log("定時器銷燬");
}
}
}
web.xml檔案中配置如下
<listener>
<listener-class>com.icss.ebu.ami.report.system.core.ClearTimerListener</listener-class>
</listener>