SpringBoot整合Quartz框架
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@EnableScheduling
@SpringBootApplication
public class QuartzTestApplication {
public static void main(String[] args) {
SpringApplication.run(QuartzTestApplication.class, args);
}
}
- 代码示例 — 一定要有@Component,一定要将该类交给Spring容器去管理,不然定时任务是不生效的
@Slf4j
@Component
public class QuartzTask {
private Logger logger = LoggerFactory.getLogger(QuartzTask.class);
@Scheduled(cron = "0/2 * * * * ? ")
public void testCron(){
logger.info("Scheduled 执行一次定时任务");
}
}
- 结果