SSM定时任务(spring3.0)

时间:2023-03-10 04:49:22
SSM定时任务(spring3.0)

SSM定时任务主要分为两部分

1.applicationContext.xml配置文件设置

设置如下:

在xmlns中添加:xmlns:task="http://www.springframework.org/schema/task"

在xsi:schemaLocation中添加:

http://www.springframework.org/schema/task

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

添加扫描:

<task:annotation-driven/>

具体如图:

SSM定时任务(spring3.0)

2.java类编辑(方法一定要是无参的)

/**
* @author xiaohan
* @create 2019/12/16 0016
*/
package com.etone.project.modules.hotspot.scheduled; import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component; @Component
@EnableScheduling
@Lazy(false)
public class Scheduled { @org.springframework.scheduling.annotation.Scheduled(cron = "0/5 * * * * ?")
public void Scheduled(){
System.out.println("任务开始美五秒执行一次");
}
}
如图:

SSM定时任务(spring3.0)

SSM定时任务(spring3.0)

注cron参数:(参数一定要正确不然会报参数错误 only no-arg methods may be annotated with @Scheduled)

// CRON表达式 含义
// “0 0 12 * * ?” 每天中午十二点触发
// “0 15 10 ? * *” 每天早上10:15触发
// “0 15 10 * * ?” 每天早上10:15触发
// “0 15 10 * * ? *” 每天早上10:15触发
// “0 15 10 * * ? 2005” 2005年的每天早上10:15触发
// “0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发
// “0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
// “0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
// “0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发
// “0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
// “0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发

以上为自己开发所记录,有些描述的不是很到位。若有疑问可添加QQ力所能及之事会尽量帮助:2898806228