1.我们先在我们的pom文件中引入依赖
<!--定时器依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2.我们需要创建一个任务类
在类的上方加入一个帮我们创建对象的注解 在方法上面加上开启定时器的注解 cron = " " 里面根据自己需求定义启动的时间 可以从https://cron.qqe2.com/ 网站取
@Component //让spring容器帮我们创建此类的对象
public class MyQuaryz {
//用来开启定时任务
@Scheduled(cron = "0/5 * * * * ?")
public void task(){
System.out.println("吃饭,睡觉,打豆豆");
}
}
3.给启动类加上开启定时器注解@EnableScheduling
@SpringBootApplication
@MapperScan("com.xjx.springbootdept.mapper")
@EnableScheduling //开启定时器的注解
public class SpringBootDeptApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDeptApplication.class, args);
}
}