SSM框架SpringMVC@Scheduled注解简单实现定时任务

第一步:

在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秒执行一次

上图:

经验分享 程序员 微信小程序 职场和发展