在项目开发中,经常需要定时任务来帮助我们来做一些内容,比如定时派息、跑批对账、业务监控等。Spring Boot 体系中现在有两种方案可以选择,第一种是 Spring Boot 内置的方式简单注解就可以使用,当然如果需要更复杂的应用场景还是得 Quartz 上场,Quartz 目前是 Java 体系中最完善的定时方案。
首先来看看 Spring Boot 自带的定时方案。
Spring Boot 内置定时
pom 包配置
pom 包里面只需要引入 Spring Boot Starter 包即可,Spring Boot Starter 包中已经内置了定时的方法。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
启动类开启定时
在启动类上面加上 @EnableScheduling 即可开启定时:
@Spring BootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Applicatio