關於TimerTask定時任務

TimerTask不是由spring管理的,所以你TimerTask內部的service也無法自動注入,2種解決辦法,1、TimerTask交由spring管理;2、通過applicationContext.getBean()獲取service.

我這裏採取了項目啓動時,使用listenr注入service。

public void contextInitialized(ServletContextEvent arg0) {
    Service s = WebApplicationContextUtils
                .getWebApplicationContext(arg0.getServletContext())
                .getBean(S.class);
    timerTask.init(s);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章