Springboot定时任务@Scheduled
在Service层新建一个类
@Service
public class SchedulePackage {
@Autowired
private TestService testService;
@Scheduled(cron = "0 0 23 * * ?")
public void PackageStore() {
List<PackageTotal> packageDatabase1= new CopyOnWriteArrayList<>();
List<PackageTotal> packageDatabase2= new CopyOnWriteArrayList<>();
DataSource dataSource1 = new DataSource("192.168.31.5");
packageDatabase1=testService.packageTotalLists(dataSource1);
DataSource dataSource2 = new DataSource("192.168.31.18");
packageDatabase2=testService.packageTotalLists(dataSource1);
}
}
在启动类添加@EnableScheduling,启用Spring的计划任务执行功能,这样可以在容器中的任何Spring管理的bean上检测@Scheduled注解,执行计划任务