SpringBoot定时执行任务

mac2025-05-17  21

启动类添加注解@EnableScheduling

@SpringBootApplication @EnableScheduling public class WebServiceApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(WebServiceApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // 注意这里要指向原先用main方法执行的Application启动类 return builder.sources(WebServiceApplication.class); } }

执行定时任务

@Component public class ScheduledTasks { @Autowired ReportIpService mReportIpService; @Scheduled(fixedRate = 5 * 60 * 1000) public void reportCurrentTime() { mReportIpService.reportService(); } }
最新回复(0)