Spring3.X 配置(註解)定時任務

Spring配置文件頭部:
<?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:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.2.xsd

        

        http://www.springframework.org/schema/task  

           http://www.springframework.org/schema/task/spring-task-3.0.xsd

        "

       xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:context="http://www.springframework.org/schema/context"

      

       xmlns:task="http://www.springframework.org/schema/task"  >

 

       <tx:annotation-driventransaction-manager="transactionManager" />

       <!-- 啓用spring註解支持 -->

       <context:annotation-config />

       <!-- 定時器開關-->

       <task:annotation-driven/>

<!-- 自動掃描 -->

       <context:component-scanbase-package="com.mx.dao" /> 

       <context:component-scanbase-package="com.mx.service" />

以上紅色部分是需要添加的。

 

另外就開始創建你的java文件(放在com.mx.dao或者com.mx.service)如下:

 

@Component

public class Task{

       @Resource(name="articleService")

       private ArticleServiceInter articleService;

      

       @Scheduled(cron="0 00 17 * *?")

       public void updateTheme(){

              articleService.updateArticleThemeForTask("tj");

       }

}

 

重點注意註解部分。java文件一定要放在註解掃描的包下面。方法名稱必須是spring中配置的事物方法。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章