zoukankan      html  css  js  c++  java
  • spring自带定时器

    http://www.cnblogs.com/pengmengnan/p/6714203.html

    注解模式的spring定时器
    1 , 首先要配置我们的spring.xml
    xmlns 多加下面的内容、
    xmlns:task="http://www.springframework.org/schema/task 

    然后xsi:schemaLocation多加下面的内容、
    1. http://www.springframework.org/schema/task 
    2. 
    http://www.springframework.org/schema/task/spring- 
    task-3.1.xsd 
    2,task任务扫描注解
    <task:annotation-driven/>
    3,配置扫描位置是:
    <context:annotation-config/> 
    <bean 
    class="org.springframework.beans.factory.annotation.Au 
    towiredAnnotationBeanPostProcessor"/> 
    <context:component-scan base- 
    package="com.jk.spring"/> 
    4 ,写一个接口
    public interface SpringHorodateurI {
    public void myTest(); 
    }
    5,接口实现类
    //import 
    org.springframework.scheduling.annotation.Scheduled;
    //import org.springframework.stereotype.Component;

    @Component //import 
    org.springframework.stereotype.Component;
    public class SpringHorodateur implements 
    SpringHorodateurI{

    @Scheduled(cron="0/5 * * * * ? ") //每5秒执行一 
    次 
    @Override 
    public void myTest() {
    System.out.println(">>>>>>>>>>>>>进入测 
    试!!");
    }

    }

    完成!
    需要注意的几点:
    1、spring的@Scheduled注解 需要写在实现上、
    2、 定时器的任务方法不能有返回值(如果有返回值,spring初始 
    化的时候会告诉你有个错误、需要设定一个proxytargetclass的 
    某个值为true、具体就去百度google吧)
    3、实现类上要有组件的注解@Component


    配置文件格式的定时器
    1、spring的配置文件
    <beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
    instance" 
    xmlns:p="http://www.springframework.org/schema/p" 

    xmlns:task="http://www.springframework.org/schema/task 


    xmlns:context="http://www.springframework.org/schema/c 
    ontext" 

    xmlns:aop="http://www.springframework.org/schema/aop" 


    xsi:schemaLocation="http://www.springframework.org/sch 
    ema/beans 

    http://www.springframework.org/schema/beans/spring- 
    beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx- 
    3.0.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee- 
    3.0.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"> 

    <task:annotation-driven /> <!-- 定时器开关--> 
    <bean id="myTaskXml" 
    class="com.spring.task.MyTaskXml"></bean> 
    <task:scheduled-tasks> 
    <!-- 
    这里表示的是每隔五秒执行一次 
    --> 
    <task:scheduled ref="myTaskXml" method="show" 
    cron="*/5 * * * * ?" /> 
    <task:scheduled ref="myTaskXml" method="print" 
    cron="*/10 * * * * ?"/> 
    <!-- 和注解的区别-->
    </task:scheduled-tasks> 
    <!-- 自动扫描的包名 --> 
    <context:component-scan base- 
    package="com.spring.task" /> 

    </beans> 
    2、基于xml的定时器任务
    /** 
    * 基于xml的定时器 
    * @author hj 
    */ 
    public class MyTaskXml { 
    public void show(){ 
    System.out.println("XMl:is show run"); 


    public void print(){ 
    System.out.println("XMl:print run"); 

    }

    CRON表达式 含义 
    "0 0 12 * * ?" 每天中午十二点触发 
    "0 15 10 ? * *" 每天早上10:15触发 
    "0 15 10 * * ?" 每天早上10:15触发 
    "0 15 10 * * ? *" 每天早上10:15触发 
    "0 15 10 * * ? 2005" 2005年的每天早上10:15触发 
    "0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟一次触 
    发 
    "0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟 
    一次触发 
    "0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55 
    分两个时间段内每5分钟一次触发 
    "0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发 
    "0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发 
    "0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五 
    的10:15触发

  • 相关阅读:
    Pycharm 设置python文件自动生成头部信息模板
    Python3.0 调用HTMLTestRunner生成的报告中不能显示用例中print函数的输出
    Python3.0 操作MySQL数据库执行SQL语句
    js时间戳转为日期格式
    Vue脚手架(vue-cli)安装总结
    Vue的生命周期
    常用的ES6语法
    学习python登录demo
    CSS的垂直居中和水平居中总结
    CSS清除浮动方法总结
  • 原文地址:https://www.cnblogs.com/xiaohouzai/p/7350994.html
Copyright © 2011-2022 走看看