Spring定时任务的几种实现

参考地址: http://gong1208.iteye.com/blog/1773177

  用的Spring Boot 使得配置变得简化,无需在其他地方加入任何配置文件;

  定时任务的代码可以写在controller层,也可以写在service,代码实现一样,具体根据实际业务来看,我的实现业务直接在服务层实现;

  定时任务的关键字是 @EnableScheduling (具体自行百度)

/**
 * 定时任务配置类
 */
@Configuration
@EnableScheduling // 启用定时任务
public class SchedulingConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    
    @Resource
    private BidMapper bidMapper;

    @Scheduled(cron = "0/20 * * * * ?") // 每20秒执行一次
    public void scheduler() {
        bidMapper.updateForTimer();
        logger.info(">>>>>>>>>>>>> 定时任务执行成功 ... ");
    }
}
原文地址:https://www.cnblogs.com/xx0405/p/6768464.html