springMVC定时任务之springSchedule的启用

xiaoxiao2021-02-28  32

一、需要了解cron表达式

1、second

2、minute

3、hours

4、day-of-mouth(月内日期)

5、mouth

6、day-of-week(周内日期)

7、year(可选)

了解特殊字段:, *  - / L w C # 的用法

网上有  cron表达式生成器 可以使用工具

二、在applicationContext.xml中添加

<!--添加定时任务--> <task:annotation-driven/>

注意:截图很重要,因为有多个annotation-driven  用 /schema/task下的

三、新建类task   类前需要加上@component注解    将新建的类注入到spring容器中(但是我测试的时候加上注解没有效果,所以,就在applicationContext.xml中以<bean>的形式注入到spring容器中)上代码:

 

import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @Slf4j public class taskTest { @Scheduled(cron = "*/30 * * * * ?") //30秒更新 public void test(){ log.info("定时器开始"); for(int i=0;i<1000;i++){ if (i0==0){ log.info("i="+i+""); } } log.info("定时器结束"); } }

转载请注明原文地址: https://www.6miu.com/read-2622983.html

最新回复(0)