zoukankan      html  css  js  c++  java
  • SpringBoot中Scheduled代码实现

    新建Maven项目SpringQuartz。

    1.1 添加依赖

    由于spring-boot-starter-web并没有依赖spring-conext-support,所以需要单独添加此依赖。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
    </dependencies>
    

      

    1.1 创建启动类

    新建com.bjsxt.QuartzApplication启动类

    @SpringBootApplication
    @EnableScheduling
    publ class QuartzApplication {
        public static void main(String[] args) {
            SpringApplication.run(QuartzApplication.class,args);
        }
    }

    1.1 新建定时任务

    新建com.bjsxt.scheduled.DemoScheduled类。

    注意类上有@Component注解,启动项目就需要加载类及类中的@Scheduled注解。

    @Component
    public class DemoScheduled {
        @Scheduled(cron="0/2 * * * * *")
        public void testScheduled(){
            System.out.println("test scheduled");
        }
    }
    

      

  • 相关阅读:
    Swift3.0 数组(Array)
    Swift3.0 UICollectionView简单使用
    Swift3.0 字符串(string)
    Swift3.0 元组 (tuples)
    Swift3.0 UICollectionView 删除,拖动
    Swift3.0 控制流
    Swift3.0 UITextView写反馈界面
    HashMap JDK1.8实现原理
    Volatile的详解
    阻塞队列之LinkedBlockingQueue
  • 原文地址:https://www.cnblogs.com/vincentmax/p/14335554.html
Copyright © 2011-2022 走看看