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);
}