Spring-Task 创建定时任务

来源:互联网 发布:ni 6229数据手册 编辑:程序博客网 时间:2024/05/17 08:13

本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种

形式,下面将分别介绍这两种方式。

第一种:配置文件方式

第一步:编写作业类

即普通的pojo,如下:

import org.springframework.stereotype.Service;  @Service  public class TaskJob {            public void job1() {          System.out.println(“任务进行中。。。”);      }  }  
第二步:在spring配置文件头中添加命名空间及描述

<beans xmlns="http://www.springframework.org/schema/beans"      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.0.xsd">  
第三步:spring配置文件中设置具体的任务
<task:scheduled-tasks>           <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   </task:scheduled-tasks>  
<context:component-scan base-package=" com.gy.mytask " />  


结果:每秒钟都打印“任务进行中……”

0 0