在springmvc.xml配置文件中,添加如下配置:
在bean下
xmlns:task="http://www.springframework.org/schema/task"
然后在 xsi下添加:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
开启定时任务扫描:
<task:annotation-driven/> <context:component-scan base-package="com.yyg.timer"/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <task:scheduler id="myScheduler" pool-size="10"/>
Java代码:
@Component
public class InsertVideoInfoJob {
@Autowired
private WXVideoReadyDao readyVideoDao;
@Scheduled(cron = "0 5 0 ? * TUE")
public void insertVideoInfo () {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//获取未推送的最早的一条记录
WXVideoReadyModel param = new WXVideoReadyModel();
List<WXVideoReadyModel> list = readyVideoDao.select(param);
System.out.println("已获取到微推送视频");
if(list.size() > 0){
for (WXVideoReadyModel model : list) {
System.out.println("视频信息已插入视频表");
//更新视频预备表---视频已推送
WXVideoReadyModel alreadyPut = model;
alreadyPut.setIsInput(1);
readyVideoDao.update(alreadyPut);
System.out.println("更新视频预备表");
}
}
}
}
到此为止就好了。
其实开始的时候我在springmvc.xml文件下 并没有加
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <task:scheduler id="myScheduler" pool-size="10"/>
所以启动会报错但是定时任务依旧会执行。