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

本文介绍如何使用Spring框架配置定时任务,包括通过注解和配置文件两种方式实现,并提供了具体的XML配置示例及Java代码示例。

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值