详解在Spring3中使用注解(@Scheduled)创建计划任务

网友投稿 219 2023-05-27


详解在Spring3中使用注解(@Scheduled)创建计划任务

Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:

创建一个java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下;

在Spring配置文件中添加三个节点;

最后说明一下,第一步创建的Java类要成为spring可管理的Bean,可以直接写在XML里,也可以@Component一下

示例如下

计划任务类:

/**

* com.zywang.spring.task.SpringTaskDemo.java

* @author ZYWANG 2011-3-9

*/

package com.zywang.spring.task;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

/**

* Spring3 @Scheduled 演示

* @author ZYWANG 2011-3-9

*/

@Component

public class SpringTaskDemo {

@Scheduled(fixedDelay = 5000)

void doSomethingWithDelay(){

System.out.priLqXFzTwyntln("I'm doing with delay now!");

}

@Scheduled(fixedRate = 5000)

void doSomethingWithRate(){

System.out.println("I'm doing with rate now!");

}

@Scheduled(cron = "0/5 * *LqXFzTwy * * *")

void doSomethingWith(){

System.out.println("I'm doing with cron now!");

}

}

Spring配置文件:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:task="http://springframLqXFzTwyework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:task="http://springframLqXFzTwyework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ES6新特性之Symbol类型用法分析
下一篇:ES6使用let命令更简单的实现块级作用域实例分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~