1、在Spring配置文件中添加
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
<context:component-scan base-package="lct.conference.timer" />
<!-- 开启注解调度支持 @Async异步-->
<task:executor id="executor" pool-size="5" />
<task:annotation-driven executor="executor"/>
2、
package lct.conference.timer; import lct.conference.util.PCMSLog; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * @Author * @Date 2020/10/26 14:18 * @Version 1.0 * @Description 描述 */ @EnableAsync @Lazy(false)//避免spring懒加载导致无效 @Component public class FlightTrainTask { PCMSLog pcmsLog = PCMSLog.getlog(getClass()); @Async//开启异步,避免阻塞 @Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行 public void taskCycle() throws InterruptedException { Thread.sleep(1000*10); pcmsLog.info("=======================使用SpringMVC框架配置定时任务"); } }