springMVC定时任务之springSchedule的启用

一、需要了解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 (i%100==0){
                log.info("i="+i+"");
            }
        }
        log.info("定时器结束");
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值