javascript
SpringMVC框架使用注解执行定时任务
2019獨角獸企業重金招聘Python工程師標準>>>
在項目開發過程中,免不了會有一些定時任務。今天就給大家一個SpringMVC框架中利用注解的方式執行定時任務的示例代碼
使用到的JAR文件:
點擊下列Jar文件會跳到我的網盤下載
aopalliance-1.0.jar
commons-logging-1.1.3.jar
spring-aop-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
spring-webmvc-3.2.4.RELEASE.jar
首先要配置我們的SpringMVC文件
????xmlns 加下面的內容、
xmlns:task="http://www.springframework.org/schema/task"????然后xsi:schemaLocation加下面的內容、
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd????最后是我們的task任務掃描注解
<!--?task任務掃描注解?--> <task:annotation-driven/>????我配置的掃描位置是
<context:component-scan?base-package="com.wuzhut"></context:component-scan>
下面寫出一個測試類
package?com.wuzhut.task;import?org.springframework.scheduling.annotation.Scheduled; import?org.springframework.stereotype.Component;@Component public?class?MyTask?{@Scheduled(cron="0/5?*?*?*?*???")?//間隔5秒執行public?void?taskCycle(){System.out.println("無主題(www.wuzhuti.cn)?<span?style="color:?#000000;">專注于前端開發技術和<span?id="3_nwp"?style="width:?auto;?height:?auto;?float:?none;"><a?id="3_nwl"?href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=d1b169b675029816&k=%B3%CC%D0%F2%BF%AA%B7%A2&k0=%B3%CC%D0%F2%BF%AA%B7%A2&kdi0=0&luki=4&n=10&p=baidu&q=06003100_cpr&rb=0&rs=1&seller_id=1&sid=16980275b669b1d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1948625&u=http%3A%2F%2Fwuzhuti%2Ecn%2F850%2Ehtml&urlid=0"?target="_blank"?mpid="3"?style="text-decoration:?none;"><span?style="color:#0000ff;font-size:12px;width:auto;height:auto;float:none;">程序開發</span></a></span>研究的技術博客</span>");} }注意
需要注意的幾點:
1、spring的@Scheduled注解 ?需要寫在實現上、
2、 定時器的任務方法不能有返回值(如果有返回值,spring初始化的時候會告訴你有個錯誤、需要設定一個proxytargetclass的某個值為true、具體就去百度google吧)
3、實現類上要有組件的注解@Component
4、下面的文章鏈接是corn表達式、大家可以參考一下
示例代碼下載地址
點擊下載
文章鏈接
quartz CronExpression表達式
轉載于:https://my.oschina.net/u/1866821/blog/482651
總結
以上是生活随笔為你收集整理的SpringMVC框架使用注解执行定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios7 适配
- 下一篇: 轻松搞定个人虚拟桌面部署之4-配置远程访