备注:这个是基于搭建好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

Spring定时任务执行的更多相关文章

  1. Spring整合Quartz定时任务执行2次,Spring定时任务执行2次

    Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...

  2. 解决spring定时任务执行2次和tomcat部署缓慢的问题

    spring定时任务执行2次 问题重现和解析 最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时间任务执行了多次.经过搜索发现是服务器上tomcat的配置文 ...

  3. spring定时任务执行两次的原因与解决方法

    spring定时任务,本地执行一次,放到服务器上后,每次执行时会执行两次,原因及解决办法. http://blog.csdn.net/yaobengen/article/details/7031266 ...

  4. spring 定时任务执行2次

    eclipse 上定时任务执行没有问题,生产环境可以看到定时任务同时执行了2次,排除代码原因,网上找了些资料,最后发现是tomcat的原因, Host 节点中有一个appBase 属性指向了webap ...

  5. spring 定时任务执行两次解决办法

    在web.xml中同时配置了ContextLoaderListener和DispatcherServlet?假如真是这样的话,需要删掉一个配置,因为你相当于配置了两个spring容器,两个容器分别都执 ...

  6. spring定时任务执行两次

    最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...

  7. spring定时任务执行两次 项目重复初始化 项目启动两次

    tomcat/config/server.xml中Host标签Context节点的问题 项目里quartz定时器总是被执行2次,通过打印发现原来项目被加载了两次,所以项目下的Listener被重复加载 ...

  8. 动态改变Spring定时任务执行频率

    @Component@EnableSchedulingpublic class updateCronTask implements SchedulingConfigurer { public stat ...

  9. Tomcat启动后加载两次web.xml的问题(因为spring定时任务执行了俩次,引出此问题)

    http://www.linuxidc.com/Linux/2011-07/38779.htmhttp://jingyan.baidu.com/article/48206aeaf9422e216ad6 ...

随机推荐

  1. linux下connect超时时间探究

    最近在linux做服务器开发的时候,发现了一个现象:服务器在启动的时候调用了 connect 函数,因为连接了一个不可用的端口,导致connect最后报出了 “Connection timed out ...

  2. (转) Linux下配置nfs并远程挂载

    nfs是网络文件系统,允许一个节点通过网络访问远程计算机的文件系统,远程文件系统可以被直接挂载到本地,文件操作和本地没有区别,如果是局域网的nfs那么io的性能也可以保证,下面就以CentOS 7.x ...

  3. 结对作业——四则运算 Part3. 对于结对编程的总结与思考

    结对作业——四则运算 Part3. 对于结对编程的总结与思考 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 Part 1. Core代码编写部分 Part ...

  4. Linux实战教学笔记36:PHP服务缓存加速深度优化实践

    一,PHP缓存加速器介绍与环境准备 1.1 PHP缓存加速器介绍 1.1.1 操作码介绍及缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(Ope ...

  5. 【LA3713 训练指南】宇航员分组 【2-sat】

    题意 有A,B,C三个任务要分配给n个宇航员,其中每个宇航员恰好要分配一个任务.设所有n个宇航员的平均年龄为x,只有年龄大于或等于x的宇航员才能分配任务A:只有年龄严格小于x的宇航员才能分配任务B,而 ...

  6. Listview getItemViewType的使用

    ListView中有两个可以用来让ListView可以在视图中显示多种布局的方法,分别是getItemType和getViewTypeCount 其中 getItemViewType返回的是有参数po ...

  7. make: *** No rule to make target `build', needed by `default'. Stop.

    [root@xx nginx-1.8.0]# makemake: *** No rule to make target `build', needed by `default'.  Stop. [ro ...

  8. mysql5.6新建的user为啥会看到没有授权过的db?

    数据库版本Server version: 5.6.22-log MySQL Community Server (GPL) root用户登录后,新建db,叫test2mysql> create d ...

  9. tyvj P3737 逐个击破

    http://tyvj.cn/p/3737 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 秉承伟大军事家的战略思想,作为一个有智慧的军长你,遇到了一个类似的 ...

  10. js运算浮点数

    在js中做小数:9.3+0.3会发现,得到的结果并不是9.6,而是9.600000000000001.这是为什么? Javascript采用了IEEE-745浮点数表示法,这是一种二进制表示法,可以精 ...