第一步sprinmvc.xml中添加一下内容:
在xmlns下添加:
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
添加后效果图如下:
第二步:添加设置定时任务代码
需添加到springmvc.xml文件中间位置
<!-- 设置定时任务 -->
<task:annotation-driven/>
第三步:新建定时任务类
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
/**
* @author 席海斌.信息部
* @date 2018年9月25日09:28:51
*/
@Controller
public class Test {
/**
* 每天7,8点每隔5分钟刷新一次
*/
@Scheduled(cron = "0 0/5 7,8 * * ?")//每天7,8点每隔5分钟刷新一次 //(0 0/20 * * * ?)每隔20分钟执行一次
public void fangfaming(){
System.out.println("定时任务一已执行!");
}
}
注:Test定时任务类必须放在配置的包扫描下
任务建立好后,需要访问该项目的一个链接后才会启动。
定时任务的执行时间设置可以使用crom表达式在线生成器生成,例如:http://cron.qqe2.com/