Loading

springboot-定时执行任务

不需要导入包运行主类

package com.lu;

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

@EnableScheduling//开启注解的定时功能
@EnableAsync//开启异步功能
@SpringBootApplication
public class SpringbootTasksApplication {

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

}

测试方法

package com.lu.service;

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

@Service
public class ScheduleService {
    //在特定的时间里执行这个方法

    //cron表达式
    //秒 分 时 日 月 周几
    @Scheduled(cron = "0 28 15 * * ?")
    public void hello(){
        System.out.println("你被执行了");
    }
}

posted @ 2022-11-19 20:15  LL。。。  阅读(14)  评论(0)    收藏  举报  来源