比如数据库中有些表中的数据要求保存15天,那15天之前的数据就会清除,可以在每天的比如12时调用方法清除15天之前的数据,这时就可以用定时任务来完成。
Spring Boot 创建定时任务
Spring Boot 默认已经实现了定时任务,只需要添加相应的注解即可完成
1.
pom.xml 文件配置
pom.xml 不需要添加其他依赖,只需要加入 Spring Boot 依赖即可,这里我们添加一个 web
和 test
的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</depende