Spring Boot定时任务实例

SpringBoot中定时任务比较简单,就2个步骤:

1. 通过@EnableScheduling激活上下文中的所有定时任务;

2. 通过@Scheduled标注某个方法为定时任务。

实例:

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration

@EnableScheduling

public class UserScheduleTaskConfig {

}

@Component

public class UserScheduleTask {

@Autowired

private UserService userService;

/**

* 用户数任务:每5分钟执行1次

*/

@Scheduled(cron = "0 0/5 * * * ?")

public void calUserCntTask() {

Integer userCnt = userService.calUserCnt();

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容