springboot_注解式开发Quartz定时任务

SpringBoot自带schedule

1.依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

2.代码

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;

@EnableScheduling
@Configuration
@Slf4j
public class TaskTimer {

@Scheduled(cron = "0/5 * * * * ?")
public void myTimes() {
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.info("我来了:::"+dateFormat1.format(new Date()));
}
}

3.解析

@EnableScheduling:作用是启动scheduling这个功能,这个可以放在启动类上面就不用每个类都写了
@Configuration:放在类上面,用于定义配置类,可替换xml配置文件,
被注解的类内部包含有一个或多个被@Bean注解的方法,
这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,
并用于构建bean定义,初始化Spring容器。
@Scheduled(cron = "0/5 * * * * ?"):用于方法上,每5秒执行一次
原文地址:https://www.cnblogs.com/chwl-ljx/p/15371789.html