Springboot开启定时任务

package com.example.scheduldemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SchedulDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchedulDemoApplication.class, args);
    }

}

package com.example.scheduldemo.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class SchedulTest {

    @Scheduled(cron = "0/5 * * * * ?")
    public void SayHello(){
        System.out.println("Hello Schedul:"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss:SSS")));
    }
}
posted @ 2020-05-20 21:38  xl4ng  阅读(247)  评论(0编辑  收藏  举报