在SpringMVC最小demo創建基礎上添加註解式定時任務
最小demo創建鏈接地址:http://blog.csdn.net/cheney1993/article/details/70162091
首先添加定時器相應jar,如下
1、添加一個定時執行的類,如下
2、ManageSql.java對應代碼:
package com.axb.cheney.task;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 心跳更新。啓動時執行一次,之後每隔2秒執行一次
* @Scheduled(fixedRate = 1000*2)
*/
@Component
public class ManageSql {
@Scheduled(cron="0/30 * * * * ? ") //間隔5秒執行
public void test(){
System.out.println("定時任務開始啦,哈哈哈");
System.out.println("<<<---------結束執行HR數據同步任務--------->>>");
}
}
3.1、xmlns 加下面的內容:
xmlns:task="http://www.springframework.org/schema/task"
3.2、然後xsi:schemaLocation加下面的內容:
http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.1.xsd
3.3、最後是我們的task任務掃描註解
<task:annotation-driven/>
3.4、配置的掃描位置是
<context:component-scan base-package="com.axb.cheney.task"></context:component-scan>
添加後,如下圖所示:
4、運行結果: