SpringBoot:整合SpringTask
一.pom.xml
<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> </dependency>
二.任务类
@Service public class TaskService { @Scheduled(initialDelay = 10000,fixedDelay = 1000) public void myTask(){ System.out.println("数据库备份时间"+ LocalDateTime.now()); } }
三.springboot主应用
@SpringBootApplication @ComponentScan(value="com.wj") @EnableScheduling//开启对Scheduled注解的支持 public class SpringtaskApplication { public static void main(String[] args) { SpringApplication.run(SpringtaskApplication.class, args); } }
四.运行结果