第一步:
在Springmvc的xml中加入如下:
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
第二步:
在中间加上:
<!-- 任务调度器 -->
<task:scheduler id="scheduler" pool-size="10" />
<!--开启注解调度支持 @Scheduled -->
<task:annotation-driven executor="scheduler" proxy-target-class="true"/>
例如:

第三步:
编写测试类:需要在类加@Component注解,但是本人在@Controller测试可行,@Component反而无法导入@Scheduled包不知为何,但是强制导入也可行,如:

/**
* 每天晚上23点执行查询第三方数据任务
* @throws Exception
*/
@Scheduled(cron = "0 0 23 * * ?")
public void Scheduled() throws Exception{
System.out.println("每天的23:00时间到了,开始调用第三方接口查询流水任务咯");
}
说明:(cron = "0 0 23 * * ?")可自行百度,如:(cron = "0/5 * * * * ?")为每5秒执行一次
上图:


本文介绍如何在Spring MVC框架中配置任务调度器,并通过@Scheduled注解实现定时任务。文章详细展示了配置步骤,包括引入命名空间、定义任务调度器及开启注解支持等。
827

被折叠的 条评论
为什么被折叠?



