一、spring配置文件中增加對這個注解的支持:
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- 開啟定時任務 -->
<task:annotation-driven />
<!-- 開啟注解 -->
<context:annotation-config />
<!-- 指定相關的包路徑 -->
<context:component-scan base-package="com.xxx.task"/>
<!-- <bean id="myTask" class="com.xxx.task.MyTask" />
<task:scheduled-tasks>
<task:scheduled ref="myTask" method="uploadFileToFTP" cron="0 29 17 * * ?" />
</task:scheduled-tasks> --><!-- 此處注釋掉是因為測試注解的時候無效,所以使用xml配置測試,發現通過,發現MyTask類需要加上注解@EnableScheduling-->
</beans>
二、MyTask類文件需要以下兩個注解
@Component("myTask")
@EnableScheduling
三、需要定時調度的方法需要以下注解,表達式根據自己需要配置
@Scheduled(cron="0 35 17 * * ?")