springboot定时任务

创建定时任务类
@Component
public class ScheduledDemo {


    /*
    * 定时任务
    *  @Scheduled  设置定时任务
    * cron属性:cron表达式,时间的字符串表达式
    * 秒 分 时 天 月  年
    * */

    @Scheduled(cron = "0/2 * * * * ?")
    public void scheduledMethod(){
        System.out.println("定时器触发:"+new Date());
    }
}


在项目启动类上面加@EnableScheduling的注解
@SpringBootApplication
@EnableScheduling
public class DistributionApplication {

    public static void main(String[] args) {
        SpringApplication.run(DistributionApplication.class, args);
    }

}

版权声明:本文为Cza_of_north原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。