1. 配置项目的配置文件(application.yml)
scheduling:
enabled: true
2.创建定时任务配置类
package com.eleven.config;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
@EnableScheduling
@ConditionalOnProperty(prefix = "scheduling", name = "enabled",havingValue = "true")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SchedulingConfig {
private final UserService userService;
@Scheduled(cron = "0 0 0/3 * * ?")
public void getAllIncrementalData() {
userService.getAllIncrementalData();
}
}