这几天想写一个动态添加任务项目找了找Spring下的自带定时功能发现还真有,然后网上找了找资料写了个demo

写了两种方式来执行定时的任务(XML配置和注解)

先建两个普通的任务类(XML配置调用的任务类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.ebbbe.task;
 
/**
 * 基于配置xml的定时器
 * @author Lese
 */
public class TaskTest {
     
     
    public void show(){
        System.out.println("XML配置:is show run");
    }
     
    public void print(){
        System.out.println("XML配置:is print run");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.ebbbe.task;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
/**
 * 基于注解的定时器
 * @author Lese
 */
@Component
public class TestTaskAnnotation {
     
    /** 
     * 定时计算。每天凌晨 01:00 执行一次 
     */  
    @Scheduled(cron = "0 0 1 * * *"
    public void show(){
        System.out.println("注解:is show run");
    }
     
    /** 
     * 心跳更新。启动时执行一次,之后每隔2秒执行一次 
     */  
    @Scheduled(fixedRate = 1000*2
    public void print(){
        System.out.println("注解:is print run");
    }
}

在写一个测试的Main方法

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.ebbbe.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * @author Lese
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");
    }
}

spring-mvc.XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/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="TaskTest" class="com.ebbbe.task.TaskTest"></bean>
     
    <task:scheduled-tasks>
    <!-- 这里表示的是从第五秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~) -->
        <task:scheduled ref="TaskTest" method="show" cron="*/5 * * * * ?" />
        <task:scheduled ref="TaskTest" method="print" cron="*/10 * * * * ?"/>
    </task:scheduled-tasks>
    <!-- 自动扫描的包名 -->  
    <context:component-scan base-package="com.ebbbe.task" />
     
</beans>

 

Spring_Task初探(注解,XML配置)的更多相关文章

  1. 使用Spring框架入门二:基于注解+XML配置的IOC/DI的使用

    一.简述 本文主要讲使用注解+xml配合使用的几种使用方式.基础课程请看前一节. 二.步骤 1.为Pom.xml中引入依赖:本例中使用的是spring-context包,引入此包时系统会自动导入它的依 ...

  2. 2015年12月10日 spring初级知识讲解(二)最小化Spring XML配置 注解

    序,随着Spring容器管理Bean数量增加,XML文件会越来越大,而且纯手工配置XML很繁琐,Spring和JAVA都提供了一些注解方式用以简化XML配置. 目录 一.自动装配(autowiring ...

  3. Hibernate注解配置与XML配置区别

    注解配置的方式与xml很很多类似: 首先是需要加入4个jar包:hibernate-commons-annotations.jar . hibernate-annotations.jar.ejb3-p ...

  4. Spring基础篇——通过Java注解和XML配置装配bean

    自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...

  5. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  6. SSH深度历险(十一) AOP原理及相关概念学习+xml配置实例(对比注解方式的优缺点)

    接上一篇 SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP,本篇我们主要是来学习使用配置XML实现AOP 本文采用强制的CGLB代理方式 Security ...

  7. spring面向切面编程示例(xml配置形式vs@注解形式)

    一.xml配置形式 1.在Spring配置文件中增加面向切面配置当调用com.activemq.service.impl.ConsumerServiceImpl接口实现类的任意方法时执行切面类中的方法 ...

  8. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...

  9. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

随机推荐

  1. MyEclipse中Source not found的问题

    1.问题描述 在MyEclipse中想查看源码,结果显示:Source not found ......(大概的意思就是找不到源码包) 2.解决方案 下载相应版本的apache-tomcat-8.5. ...

  2. 13 JSON-RPC: a tale of interfaces

    JSON-RPC: a tale of interfaces 27 April 2010 Here we present an example where Go's interfaces made i ...

  3. Linux下实现ping功能

    实现ping功能,就肯定要用到ping命令,那么在Linux下ping命令为: ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面&g ...

  4. Ubuntu下软件安装方式、PATH配置、查找安装位置

    Ubuntu 18.04, 安装方式 目前孤知道的Ubuntu下安装软件方式有3种(命令): 1.make 2.apt/apt-get 3.dpkg 方式1基于软件源码安装,需要经历配置(可选).编译 ...

  5. c语言循环链表的问题

    今天,老师说了一道题,大意是,有一群小朋友10个人,但是老师只有一个苹果,只能给一个小朋友,于是老师就决定让小朋友们做成一圈,从第一个小朋友开始,每隔一个小朋友就没有机会得到苹果,最后剩下的一个人可以 ...

  6. AngularJs(SPA)单页面SEO以及百度统计应用(上)

    只有两种人最具有吸引力,一种是无所不知的人,一种是一无所知的人 问:学生问追一个女孩总是追不上怎么办?回答:女孩不是追来的,是吸引来的,你追的过程是吸引女孩的过程,如果女孩没有看上你,再追都是没有用的 ...

  7. ld 脚本浅析-LD手册粗糙翻译

    本文乃转载, 我在其基础上做了少量修改. 原作者的E-mail是zhanglei@sict.ac.cn. 完成于2005.11.5-2005.11.8 0. Contents 1. 概论2. 基本概念 ...

  8. CF 248B 前缀和

    操作1 l r 是原序列l-r的和操作2 l r 是从小到大排序后的l-r的和 input66 4 2 7 2 732 3 61 3 41 1 6output24928 # include <i ...

  9. Phoenix的安装使用与SQL查询HBase

    一. Phoenix的简介 1. 什么是phoenix 现有hbase的查询工具有很多如:Hive,Tez,Impala,Shark/Spark,Phoenix等.今天主要说Phoenix.phoen ...

  10. day6面向对象--类的特殊成员方法

    类的特殊成员方法 1. __doc__ 表示类的描述信息 class Func(object): '''__doc__方法是用来打印类的描述信息''' def tell(self): pass def ...