package com.lyd.swagger;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling //启动定时任务@SpringBootApplicationpublic class SwaggerApplication {public static void main(String[] args) {SpringApplication.run(SwaggerApplication.class, args);}}
不用刻意去调用下面的hello方法,加了@Scheduled注解,到时间了自动调用
package com.lyd.swagger.service;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class ScheduledService {//在一个特定的时间执行这个方法//cron表达式,用来表示在什么时间执行,有点反人类,网上有很多教程//秒 分 时 日 月 周几@Scheduled(cron="0 31 10 * * ?")public void hello(){System.out.println("hello,这里被执行了");}}
