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

    注解模式的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触发

  • 相关阅读:
    ASP.Net User Controls as Static or Movable PopUps
    处理WinForm多线程程序时的陷阱(摘自网络)
    《颤抖吧,无证程序员们》只为娱乐
    Javascript和CSS浏览器兼容总结
    收藏的一个c#通讯编程的帖子很全
    WEB开发人员常用速查手册
    批量修改文件名称( 收藏的一个连接)
    SQL server常用操作
    开源网站大收藏
    pragma comment的使用
  • 原文地址:https://www.cnblogs.com/pengmengnan/p/6714203.html
Copyright © 2011-2022 走看看