springboot定时任务 demo



















1. 启动类, 开启对定时任务的支持
@EnableScheduling

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

@EnableScheduling //开启对定时任务的支持
@SpringBootApplication
public class RuoyiWxApplication {

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

}

2. cron执行定时任务
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class GatherTask {


    /**
     * 定时采集行业信息
     *
     * 0 0 22 * * ?     每天晚上10点 执行任务
     */
    @Scheduled(cron="0 0 22 * * ?")
    public void gathering(){
        // TODO
        log.info("采集中...");
    }
}

你可能感兴趣的:(cron,spring,boot,spring,java)