【Springboot】——@EnableScheduling、@Scheduled

@EnableScheduling开启计划任务支持,

@Scheduled计划任务声明

复制代码

 1 package ch2.scheduler2;
 2 
 3 //日期转换方式
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 //计划任务声明
 8 import org.springframework.scheduling.annotation.Scheduled;
 9 //spring组件注解
10 import org.springframework.stereotype.Service;
11 
12 @Service
13 public class SchedulerService {
14 
15     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH::mm::ss");
16     
17     @Scheduled(fixedRate=5000)
18     public void proFixCurrentTime()
19     {
20         System.out.println("每5秒钟执行一次:" + dateFormat.format(new Date()));
21     }
22     
23     @Scheduled(cron="0 53 18 ? * *")
24     public void cornCurrentTime()
25     {
26         System.out.println("自定执行时间: " + dateFormat.format(new Date()));
27     }
28     
29     
30 }

复制代码

 

复制代码

 1 package ch2.scheduler2;
 2 
 3 //引入spring配置注解
 4 import org.springframework.context.annotation.Configuration;
 5 //引入spring自动载入注解
 6 import org.springframework.context.annotation.ComponentScan;
 7 
 8 //计划任务声明类:开启计划任务声明
 9 import org.springframework.scheduling.annotation.EnableScheduling;
10 
11 //spring配置类声明
12 @Configuration
13 //自动引入当前包下的service,component....
14 @ComponentScan("ch2.scheduler2")
15 //开启对计划任务的支持
16 @EnableScheduling
17 public class TaskSchedulerConfig {
18 
19 }

复制代码

 

复制代码

 1 package ch2.scheduler2;
 2 //引入容器
 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args)
 8     {
 9         
10         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
11         //SchedulerService schedulerService = context.getBean(SchedulerService.class);
12         
13     }
14     
15 }

复制代码

原文地址:https://www.cnblogs.com/achengmu/p/8259933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值