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

<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. python 复制

    1. list的复制 直接用赋值符号实现浅复制,两者用id()函数的返回值是相同的,也就是占用同一块内存空间. 导入 copy 库, 用 copy.deepcopy(list1) 再赋值实现深复制,两 ...

  2. 更新neo4j节点信息

    将多个属性的内容更新到节点上 def update_by_id(id,graph,**kwargs): """ 更新节点的属性 根据节点的ID来更新节点的属性,如果存在该 ...

  3. IDEA插件之JProfiler

    1.是什么?来用干嘛的? 一个商业授权的Java剖析工具. 用来剖析程序内存.CPU使用情况,找到性能瓶颈,快速定位问题所在. 2.IDEA安装JProfiler插件 (1)File -> Se ...

  4. Eureka 服务中心

    old                                                                                           使用Eure ...

  5. Windows下mysql导出和导入数据库表(命令行)

    导出: 1.打开ctrl+R输入cmd 打开命令行 2.cd D:\mysql-8.0.15-winx64\bin 到MySQL的bin目录 3.输入命令  mysqldump -u root -p ...

  6. python学习-1 编程语言的介绍

    1.开发语言: 高级语言:Java.python.C#.PHP.Go.ruby.C++等等 低级语言:C.汇编语言 2.机器码和字节码 高级语言 ====>字节码 低级语言===>机器码 ...

  7. China Union Pay helper

    static string proxyIpAddress = AppConfig.GetProxyIpAddress; static string proxyUserName = AppConfig. ...

  8. JS基础_实参可以是任何值

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. arcgis js 之 渔网工具(调用地图服务)

    arcgis js 之 渔网工具(调用地图服务) 原理: 简历不同级别的网渔网图层,设置显示比例尺.然后发布服务,使用MapImageLayer接收. 过程: 1.在arcmap中用创建渔网工具将不同 ...

  10. fetch---基本使用

    一.fetch fetch是一种XMLHttpRequest的一种替代方案,在工作当中除了用ajax获取后台数据外我们还可以使用fetch.axios来替代ajax 二.安装 执行npm instal ...