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

<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. centos7ping www.baidu.com没有ping通

    在linux中ping www.baidu.com 无法ping通,可能原因是DNS没配置好 1.修改vi /etc/resolv.conf 增加如下内容: nameserver 114.114.11 ...

  2. TCP的socket连接

    package newtest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStre ...

  3. 题目13 在O(1)时间删除链表节点

    ///////////////////////////////////////////////////////////////////////////////////// // 3. 题目13 在O( ...

  4. Photon Server初识(三) ---ORM映射改进

    一:新建一些管理类, 二.实现每个管理类 (1)NHibernateHelper.cs 类,管理数据库连接 using NHibernate; using NHibernate.Cfg; namesp ...

  5. Java打包

    打成jar包 如果你使用的是maven来管理项目,执行以下命令既可以 cd 项目跟目录(和pom.xml同级)mvn clean package## 或者执行下面的命令## 排除测试代码后进行打包mv ...

  6. AngularJS-03 过滤器

    过滤器 可以对输入的值按照指定的方案进行处理后再输出的函数. 1.货比过滤器currency:{{ currency_expression | currency : symbol}} 2.日期过滤器: ...

  7. c#OpenCVSharp+Zxing识别条形码

    参考博客:https://www.cnblogs.com/dengxiaojun/p/5278679.html,但是他的demo下载太贵了 可以下载这个https://download.csdn.ne ...

  8. JS的日期的知识与格式化

    需要知道的JS的日期的知识,都在这了 https://juejin.im/post/5d12b308f265da1b7c612746?utm_source=gold_browser_extension ...

  9. chartjs显示数值标签插件:chartjs-plugin-datalabels

    Getting Started #Installation #npm   npm install chartjs-plugin-datalabels --save This plugin can al ...

  10. CentOS7中使用yum安装Nginx的详细步骤

    1.添加源 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarc ...