Spring3.0定时任务 (一) 简单示例

2014-11-24 08:52:04 来源: 作者: 浏览: 0

在项目中经常用到的spring的一个功能就是定时任务,它可以自动监视时间,到点就执行,给程序带来了很大的方便,很多地方都会需要这种功能,比如做数据备份、同步等操作。最近一直比较忙,主要是比较懒,今天把这部分稍作小结。


使用spring定时任务的前提:项目中已经搭建好了spring环境(我用的是spring3.0)。


一、基本使用:


spring的定时任务使用起来十分方便,只需要两步:1、写好执行定时任务的类和方法;2、配置spring定时任务配置文件:


1、写好执行定时任务的类和方法:


package com.test;


public class Test {


public void test() {
System.out.println("执行定时任务的方法。");
}
}


2、配置spring的定时任务配置文件(可以新建一个xml文件,也可以在已经有的xml文件中配置)


配置文件内容:


< xml version="1.0" encoding="UTF-8" >
http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">









testTrigger"/>



 
testDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

com.test.Test">


test


 
testTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">

testDetail"/>  



0 0 23 * *






-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: