zoukankan      html  css  js  c++  java
  • 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
Copyright © 2011-2022 走看看