Spring定时任务执行

备注:这个是基于搭建好spring的环境下的

 

注解方式:

1.定时任务类

 

package com.test;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("mytask")
public class MyTask {
    int i =0;
    @Scheduled(cron = "0 0/1 * * * ?")//!!!!!表示每分钟执行一次!!!!!cron是表示触发的时间表达式
    public void doTask(){
        System.out.println("结果是:"+(i++)+":"+new Date());
    }
}

2.配置文件:schedule.xml;备注:名称随便起。还有,需要在总的配置文件映入改文件

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task" 
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
       xmlns:aop="http://www.springframework.org/schema/aop">
  
    <context:annotation-config />  
    <!-- 扫描定时任务类所在的包 -->
    <context:component-scan base-package="com.test" />  
    
 </beans> 

 

XML方式:

1.定时任务类:相比注解方式,方法上面没有注解,通过XML方式注入

@Component("mytask")
public class MyTask {
    int i =0;
    public void doTask(){
        System.out.println("结果是:"+(i++)+":"+new Date());
    }
}

2.配置文件:schedule.x

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" xmlns:aop="http://www.springframework.org/schema/aop"> <!-- <context:annotation-config /> --> <!-- 扫描定时任务类所在的包 --> <task:scheduled-tasks> <task:scheduled ref="cancelSysSeatByTask" method="doTask" cron="0 0/1 * * * ?"/>
   </task:scheduled-tasks>

</beans>

 

3.部署到tomcat并且启动,看看后台是否输出以下信息:

结果是:0:Mon Mar 30 16:23:00 CST 2015
结果是:1:Mon Mar 30 16:24:00 CST 2015
结果是:2:Mon Mar 30 16:25:00 CST 2015

 

转载于:https://www.cnblogs.com/aisam/p/4378413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值