Schedule存在spring-context.jar包中。

实现简单步骤:

1、配置bean.xml开启定时任务支持。

<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd "> <!-- 开启定时任务 -->
<task:annotation-driven />
<context:component-scan base-package="com.jsoft.testspring" /> <context:annotation-config /> <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean> </beans>

代码实现:

package com.jsoft.testspring.testmvchelloworld;

import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class ScheduleTest { @Scheduled(cron = "0/5 * * * * ?")
public void schTest1() {
Date date = new Date();
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sim.format(date);
System.out.println("这是spring定时器1,每五秒执行一次,当前时间:" + dateStr);
}
}

注意要加@Component这类的注解。

示例工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test24/testmvchelloworld

参考:

http://blog.csdn.net/tuzongxun/article/details/51576301

Spring MVC使用Schedule实现定时任务的更多相关文章

  1. 关于Spring mvc注解中的定时任务的配置

    关于spring mvc注解定时任务配置 简单的记载:避免自己忘记,不是很确定我理解的是否正确.有错误地方望请大家指出. 1,定时方法执行配置: (1)在applicationContext.xml中 ...

  2. Spring Boot使用Schedule实现定时任务

    适用的工具是:Schedule 集成步骤: 1.开启Schedule支持 package com.jsoft.springboottest.springboottest1; import org.sp ...

  3. Spring Schedule 实现定时任务

    很多时候我们都需要为系统建立一个定时任务来帮我们做一些事情,SpringBoot 已经帮我们实现好了一个,我们只需要直接使用即可,当然你也可以不用 SpringBoot 自带的定时任务,整合 Quar ...

  4. Spring 3.1新特性之二:@Enable*注解的源码,spring源码分析之定时任务Scheduled注解

    分析SpringBoot的自动化配置原理的时候,可以观察下这些@Enable*注解的源码,可以发现所有的注解都有一个@Import注解.@Import注解是用来导入配置类的,这也就是说这些自动开启的实 ...

  5. 高性能的关键:Spring MVC的异步模式

    我承认有些标题党了,不过话说这样其实也没错,关于“异步”处理的文章已经不少,代码例子也能找到很多,但我还是打算发表这篇我写了好长一段时间,却一直没发表的文章,以一个更简单的视角,把异步模式讲清楚. 什 ...

  6. Spring MVC的异步模式

    高性能的关键:Spring MVC的异步模式   我承认有些标题党了,不过话说这样其实也没错,关于“异步”处理的文章已经不少,代码例子也能找到很多,但我还是打算发表这篇我写了好长一段时间,却一直没发表 ...

  7. Java 本地开发环境搭建(框架采用 Spring+Spring MVC+Hibernate+Jsp+Gradle+tomcat+mysql5.6)

    项目搭建采用技术栈为:Spring+Spring MVC+Hibernate+Jsp+Gradle+tomcat+mysql5.6 搭建环境文档目录结构说明: 使用Intellj Idea 搭建项目过 ...

  8. Spring Boot整合Quartz实现定时任务表配置

    最近有个小项目要做,spring mvc下的task设置一直不太灵活,因此在Spring Boot上想做到灵活的管理定时任务.需求就是,当项目启动的时候,如果有定时任务则加载进来,生成schedule ...

  9. springboot学习章节代码-Spring MVC基础

    1.项目搭建. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

随机推荐

  1. centOS下SVN安装和配置

    1>SVN服务器端文件可见问题 在平时使用SVN时候,一直以为在客户提交文件,那么在服务器对应的版本库下面就会有相同文件.在自己搭建后,发现提交到服务器端文件完全看不见.... 这是由于SVN服 ...

  2. Redis数据库(二)

    1. Redis数据库持久化 redis提供了两种数据备份方式,一种是RDB,另外一种是AOF,以下将详细介绍这两种备份策略. 面试: 1.1  配置文件详解备份方式 [root@localhost ...

  3. 如何用纯 CSS 创作一个雷达扫描动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdbGvr 可交互视频 ...

  4. jsDate()

    var myTime=new Date();//myTime的数据类型为(typeof) object //下面得到的都为number 类型 getFullYear();年 四位数字返回年份. get ...

  5. Python数据分析库之pandas,你该这么学!No.1

    写这个系列背后的故事 咦,面试系列的把基础部分都写完啦,哈哈答,接下来要弄啥嘞~ pandas吧 外国人开发的 翻译成汉语叫 熊猫 厉害厉害,很接地气 一个基于numpy的库 干啥的? 做数据分析用的 ...

  6. 四、10分钟ToPandas_0.24.2

    # Author:Zhang Yuan整理,版本Pandas0.24.2 # 0. 习惯上,我们会按下面格式引入所需要的包: import pandas as pd import numpy as n ...

  7. mysql 与QT的连接

    第一步:安装 qt开发环境 bi@bi-desktop:~$sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4- ...

  8. 菜鸟的《Linux程序设计》学习—shell script

    1. 认识shell script shell script是利用shell的功能缩写的一个"程序",这个程序是使用纯文本文件,将一些shell的语法与命令(含外部命令)写在里面, ...

  9. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  10. 基础训练 2n皇后问题

    2n皇后问题 #include<iostream> #include<vector> using namespace std; int cnt = 0, n; vector&l ...