在定时任务配置文件中添加内容:

<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="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>

如果是配置式的定时任务:

package com.spring.task;

/**
* 基于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");
}
}

如果是注解式的定时任务:

package com.spring.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* 基于注解的定时器
* @author hj
*/
@Component
public class MyTaskAnnotation { /**
* 定时计算。每天凌晨 01:00 执行一次
*/
@Scheduled(cron = "0 0 1 * * *")
public void show(){
System.out.println("Annotation:is show run");
} /**
* 心跳更新。启动时执行一次,之后每隔2秒执行一次
*/
@Scheduled(fixedRate = 1000*2)
public void print(){
System.out.println("Annotation:print run");
}
}

测试:

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");
}
}

spring定时任务的配置式与注解式的更多相关文章

  1. Spring学习8-Spring事务管理(注解式声明事务管理)

    步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans& ...

  2. Spring AOP基础概念及自定义注解式AOP初体验

    对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...

  3. spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )

    在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...

  4. spring定时任务详解(@Scheduled注解)

    Spring配置文件xmlns加入 xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocati ...

  5. 《转载》spring定时任务详解(@Scheduled注解)

    本文转载自爱如指间沙 //每一个小时执行一次 @Scheduled(cron = "0 0 * * * ?") public void saveDailyScoreSchedule ...

  6. spring定时任务详解(@Scheduled注解)多线程讲解

    (一)在xml里加入task的命名空间 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  7. spring 5.x 系列第21篇 —— spring 定时任务 (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 关于任务的调度配置定义在springApp ...

  8. Spring 定时任务的配置

    1.applicationContext.xml 中 加入task 的声明与xsd ? 1 xmlns:task="http://www.springframework.org/schema ...

  9. spring定时任务的配置

    定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob&qu ...

随机推荐

  1. [LuoguP3064][USACO12DEC]伊斯坦布尔的帮派Gangs of Istanbull(加强版)_线段树_贪心

    伊斯坦布尔的帮派Gangs of Istanbull 题目链接:https://www.luogu.org/problem/P3064 数据范围:略. 题解: 这个题其实分为两问,第一问是$YES$. ...

  2. Linux上面mount 域控的目录 超时 然后提示 error的解决办法

    mount error(112): Host is down 故障解决 https://blog.csdn.net/lepton126/article/details/89447713 之前查到过 这 ...

  3. 华为CPU的类型

    主要分类: 以及 一句名言

  4. random、range和len函数的使用

    random.range和len函数的使用 一.random函数 1.random.random()和random.Random(): import random num = random.rando ...

  5. 【Funny Things】002——鞋的颜色

    网上的那张鞋子的图片到底是什么颜色的?灰绿色还是粉色? 1. 先截取图片中鞋的那块的图片,获取大小 2. 带入大小分别计算R,G,B平均值 3. 通过计算所得的数据画图可得结果 from PIL im ...

  6. 我学会了正确的dinic

    以前写Isap的时候,总是被卡,然后学了一发Isap的当前弧优化,好像可以水过很多题 但是一直没明白为啥Isap会走一个环??? 然后写dinic了,听说不容易被卡(来自去年九省联考的指导) 然而-- ...

  7. pycharm配置git版本管理

    1.下载并安装git 首先你电脑必须安装git版本控制器(软件),在官网下载即可 2.安装git,正常安装即可 编缉器的选择,根据电脑实际情况选择合适的编缉器 安装参考:https://www.cnb ...

  8. Two progressions(CodeForces-125D)【鸽巢原理】

    题意:将一列数划分为两个等差数列. 思路:首先,我要吹爆鸽巢原理!!!真的很强大的东西!!! 加入能完成题设操作,则前三个数中,必有至少两个数在同一序列,枚举三种情况(a1 a2,a2 a3,a1 a ...

  9. 整体二分(模板二)动态区间第K大

    这才是更一般的二分写法--HDU5412 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>// ...

  10. python中requests库使用方法详解

    目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...