码迷,mamicode.com
首页 > 编程语言 > 详细

springboot的定时任务

时间:2019-05-31 13:29:31      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:image   col   string   启动   注解   com   date   inf   group   

1、定时任务的创建方式:  

  •   基于注解@Schedule和,定时任务执行时间较短,并且比较单一

2、@Schedule@EnableScheduling

  (1)在pom.xml中加入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
</dependency>

  (2)在springboot的启动类DemoApplication上添加@EnableScheduling

package com.example.demo;

@SpringBootApplication @EnableScheduling
public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); System.out.println("你好"); } }

  (3)创建定时任务类

package com.example.demo.Time;

@Component
public class timer { @Scheduled(cron = "0/15 * * * * ? ") //每隔15秒执行一次 private void test(){ System.out.println("任务开始时间:"+new Date()); } }

执行效果:

  技术图片

 

springboot的定时任务

标签:image   col   string   启动   注解   com   date   inf   group   

原文地址:https://www.cnblogs.com/echola/p/10954552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!