SpringMVC的定时任务的配置

来源:互联网 发布:九二五网络 编辑:程序博客网 时间:2024/06/05 17:14

1、在spring-mvc.xml中填写如下内容:

xmlns:task="http://www.springframework.org/schema/task" 

2、在spring-mvc.xml中填写如下内容:

http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.2.xsd

3、在spring-mvc.xml中填写如下内容:

<context:component-scan base-package="com.bms.timing.task"/> 

其中:com.bms.timing.task"是自己定义的定时任务所在的包


4、定义定时任务

package com.bms.timing.task;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class MyTask {@Scheduled(cron="0/5 * * * * ? ") //间隔5秒执行  public void taskCycle(){System.out.println("hello world!!");}}

其中:@Scheduled(cron="0/5 * * * * ? ") 用来设计定时任务的执行频率

5、启动任务执行结果


0 0