SpringBoot之定时器

   同传统的Spring项目相比,在基于SpringBoot实现的项目中添加定时器十分容器,那具体怎么做呢?

1.启动类Application添加注解@EnableScheduling

2.在我们需要定时执行的方法上添加注解@Scheduled

3.实例

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

 

/**

 * 定时器

 *

 */

@Component

public class MyExecutor {

/**

* 一周执行一次 cron = "0 0 1 ? * MON" 每周一凌晨一点执行

*/

@Scheduled(cron = "0 0 1 ? * MON")

public void scanAllRequestUrls() {

//执行具体的业务

}

}

猜你喜欢

转载自williamwhj.iteye.com/blog/2356287