在Spring配置文件中进行配置基本信息
在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然后是必须的一项,因为这个是spring提供的定时任务,所以需要进行扫描类下的包才能执行相应的操作
<!-- 配置扫描的包 我把相应的测试类放到com.test包下,这个一定要写--> <context:component-scan base-package="com.test"/>然后是设置定时任务
<!-- 设置定时任务 --> <task:annotation-driven/>开始在测试类中写测试代码
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * Created by leo on 2018/1/22. */ @Component public class TimerTask { @Scheduled(cron = "* * * * * ?")//每分钟都执行 public void test(){ System.out.println("执行"); } }Cron配置
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触发