定时任务在项目中经常应用,在每天、每月生成统计和报表等都需要,本文主要在spring boot和spring中应用定时任务。
Spring Boot定时任务
- 在Spring Boot启动类标注@EnableScheduling注解
package com.edu.yang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class YangApplication {
public static void main(String[] args) {
SpringApplication.run(YangApplication.class, args);
}
}
- 开发定时任务服务类
/**
* @auther: yangyongzhen
* @date: 2018/11/24 20:47
*/
@Service
public class ScheduleServiceImpl {
@Autowired
private TransactionService transactionService;
@Scheduled(cron = "0 5 0 * * ?")
@Async
public vo