美文网首页
Spring Boot定时任务实例

Spring Boot定时任务实例

作者: Superwind20 | 来源:发表于2017-02-11 23:01 被阅读71次

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();

}

}

相关文章

网友评论

      本文标题:Spring Boot定时任务实例

      本文链接:https://www.haomeiwen.com/subject/xcgoittx.html