spring定时任务的几种配置方法

来源:互联网 发布:c语言的数据类型有哪些 编辑:程序博客网 时间:2024/06/03 17:14

首先在配置文件头部的必须要有:

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

1、Spring注解配置<task:annotation-driven/>

例子:
spring.xml配置

<context:component-scan base-package="com.hesvit" /><task:annotation-driven/>

执行任务的bean

@Component@Lazy(value=false)public class TaskTest{    @Scheduled(cron=" 0/5 * * * * ?")    public void testfun() {        System.out.println("每5s执行一次---------飞呀飞呀,我的骄傲放纵");    }}

2、Spring配置文件配置

例子:
spring.xml配置

<context:component-scan base-package="com.hesvit" /><task:scheduled-tasks>        <task:scheduled ref="taskTest" method="testfun" cron="0/3 * * * * ?"/>       </task:scheduled-tasks>

执行任务的bean

@Component@Lazy(value=false)public class TaskTest{    public void testfun() {        System.out.println("每5s执行一次---------飞呀飞呀,我的骄傲放纵");    }}

cron表达式请参考
http://www.cnblogs.com/liuyitian/p/4108391.html