最近在做定时器的时候,用到了Java的Timer,写完去跑程序的时候,运行以后发现实现类的service是null,这时候就开始考虑是不是因为项目启动以后注入没注入进去。
上网上也看了很多的方法,尝试了一下感觉都不太靠谱。后来去搜项目启动如何配置注入service,发现了解决方法:
通过写一个applicationContext的配置类,然后在自己new实现类的地方get,就可以把bean进行注入了。
这就是配置类
package cn.horizon.action;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil implements ApplicationContextAware{
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
ApplicationContextUtil.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
package cn.horizon.action;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import cn.horizon.domain.Work;
import cn.horizon.service.WorkService;
/**
*
* 每日定时查询有无打卡信息
*
*/
@Component
public class TimeWorkAction extends TimerTask{
@Autowired
WorkService workService;
public void run(){
try {
//获取当前日期
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = new Date();
String dates = simpleDateFormat.format(date1);
long currentDate = 0;
try {
currentDate = simpleDateFormat.parse(dates).getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取权限用户
List userName = workService.getName();
for (int i = 0; i < userName.size(); i++) {
Object obj = userName.get(i);
String name = obj.toString();
List addDate = workService.addDate(name,currentDate);
if(addDate.isEmpty()){
Work work = new Work();
work.setName(name);
work.setWorkDate(currentDate);
work.setState("1");
workService.save(work);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public WorkService getWorkService() {
return workService;
}
public void setWorkService(WorkService workService) {
this.workService = workService;
}
}
package cn.horizon.timer;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.context.ApplicationContext;
import cn.horizon.action.ApplicationContextUtil;
import cn.horizon.action.TimeWorkAction;
public class WorkTimer extends HttpServlet{
//时间间隔(一天)
private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;
@Override
public void init() throws ServletException {
ApplicationContext context = ApplicationContextUtil.getApplicationContext();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23); //夜晚23点
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date=calendar.getTime(); //第一次执行定时任务的时间
//这里通过applicationContext的getbean方法拿到实现类,从而实现了bean的调用
TimerTask task = (TimerTask)context.getBean(TimeWorkAction.class);
Timer timer = new Timer();
timer.schedule(task,date, PERIOD_DAY);
super.init();
}
}