@WebListener
public class DayIntervalCowGrode implements ServletContextListener{
public ScoureService scoureService;
public UserService userService;
public MPropertyService propertyService;
private CowGrodeService cowGrodeService;
public void showDayTime() {
// 时间类
Calendar startDate = Calendar.getInstance();
startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DATE), 01, 00, 0);
Timer dTimer = new Timer();
dTimer.schedule(new TimerTask() {
@Override
public void run() {
//执行代码块
}, startDate.getTime(), 24* 60* 60 * 1000);//设置执行时间每天1点,24小时执行一次
}
@Override
public void contextInitialized(ServletContextEvent sce) {
//获取spring上下文需要的对象
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
scoureService = context.getBean(ScoureService.class);
userService=context.getBean(UserService.class);
propertyService=context.getBean(MPropertyService.class);
cowGrodeService=context.getBean(CowGrodeService.class);
showDayTime();
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
Spring项目启动执行
最新推荐文章于 2024-06-12 13:24:28 发布