基於Spring task註解方式配置任務

來源:互聯網
上載者:User

           此文標題有錯,感謝各位網友指出

        林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka

        建立一個Java工程,匯入要用到的包,Spring3.2、Quartz2.2.1、aopalliance-1.0.jar、commons-logging-1.2.jar。整個工程目錄如下:


本文工程免費下載
1、配置需要調度的類,並添加註解

package com.mucfc;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;/**  *事件類別,基於Spring註解方式*作者 林炳文(ling20081005@126.com 部落格:http://blog.csdn.net/evankaka)  *時間 2015.4.29*/@Componentpublic class MyJob {    public MyJob(){        System.out.println("MyJob建立成功");    }     @Scheduled(cron = "0/1 * *  * * ? ")//每隔1秒隔行一次        public void run(){         System.out.println("Hello MyJob  "+                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));        }}
2、首先要配置我們的beans.xml,在xmlns 多加下面的內容
xmlns:task="http://www.springframework.org/schema/task"
3、然後xsi:schemaLocation多加下面的內容
    http://www.springframework.org/schema/task      http://www.springframework.org/schema/task/spring-task-3.0.xsd

4、最後是我們的task任務掃描註解

    <!--開啟這個配置,spring才能識別@Scheduled註解-->     <task:annotation-driven/> 
5、自動設定掃描位置:

     <!-- 自動掃描註解的bean -->    <context:component-scan base-package="com.mucfc"/>
6、整個文檔如下
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"    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.0.xsd">    <!--開啟這個配置,spring才能識別@Scheduled註解-->     <task:annotation-driven/>     <!-- 自動掃描註解的bean -->    <context:component-scan base-package="com.mucfc"/></beans>
7、使用
package com.mucfc;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {    public static void main(String[] args) {        // TODO Auto-generated method stub        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");    }}

輸出結果:


一旦這個xml被載入進來來,就會自動建立bean的執行個體,並且開始定時任務了
需要注意的幾點:
1、spring的@Scheduled註解  需要寫在實現上
2、 定時器的任務方法不能有傳回值(如果有傳回值,spring初始化的時候會告訴你有個錯誤、需要設定一個proxytargetclass的某個值為true、具體就去百度google吧)
3、實作類別上要有組件的註解@Component

林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka

本文工程免費下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.