Spring MVC使用Schedule实现定时任务
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实现定时任务的更多相关文章
- 关于Spring mvc注解中的定时任务的配置
关于spring mvc注解定时任务配置 简单的记载:避免自己忘记,不是很确定我理解的是否正确.有错误地方望请大家指出. 1,定时方法执行配置: (1)在applicationContext.xml中 ...
- Spring Boot使用Schedule实现定时任务
适用的工具是:Schedule 集成步骤: 1.开启Schedule支持 package com.jsoft.springboottest.springboottest1; import org.sp ...
- Spring Schedule 实现定时任务
很多时候我们都需要为系统建立一个定时任务来帮我们做一些事情,SpringBoot 已经帮我们实现好了一个,我们只需要直接使用即可,当然你也可以不用 SpringBoot 自带的定时任务,整合 Quar ...
- Spring 3.1新特性之二:@Enable*注解的源码,spring源码分析之定时任务Scheduled注解
分析SpringBoot的自动化配置原理的时候,可以观察下这些@Enable*注解的源码,可以发现所有的注解都有一个@Import注解.@Import注解是用来导入配置类的,这也就是说这些自动开启的实 ...
- 高性能的关键:Spring MVC的异步模式
我承认有些标题党了,不过话说这样其实也没错,关于“异步”处理的文章已经不少,代码例子也能找到很多,但我还是打算发表这篇我写了好长一段时间,却一直没发表的文章,以一个更简单的视角,把异步模式讲清楚. 什 ...
- Spring MVC的异步模式
高性能的关键:Spring MVC的异步模式 我承认有些标题党了,不过话说这样其实也没错,关于“异步”处理的文章已经不少,代码例子也能找到很多,但我还是打算发表这篇我写了好长一段时间,却一直没发表 ...
- Java 本地开发环境搭建(框架采用 Spring+Spring MVC+Hibernate+Jsp+Gradle+tomcat+mysql5.6)
项目搭建采用技术栈为:Spring+Spring MVC+Hibernate+Jsp+Gradle+tomcat+mysql5.6 搭建环境文档目录结构说明: 使用Intellj Idea 搭建项目过 ...
- Spring Boot整合Quartz实现定时任务表配置
最近有个小项目要做,spring mvc下的task设置一直不太灵活,因此在Spring Boot上想做到灵活的管理定时任务.需求就是,当项目启动的时候,如果有定时任务则加载进来,生成schedule ...
- springboot学习章节代码-Spring MVC基础
1.项目搭建. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
随机推荐
- DNS服务-了解篇
简介 DNS是用来名字解析的,名字解析成IP地址,IP地址解析成名字,正反操作,有服务器端和客户端即 S/C DNS是应用层协议,基于UDP/53.TCP/53端口,缺一不可 分为正向解析和反向解析/ ...
- laravel的安装与启动
今天,我就来给大家分享下laravel的安装 https://pkg.phpcomposer.com 这是官网的中国镜像 第一步: 点链接进来执行下面的三条语句 执行完后,查看下当前目录底下有个 c ...
- 四、10分钟ToPandas_0.24.2
# Author:Zhang Yuan整理,版本Pandas0.24.2 # 0. 习惯上,我们会按下面格式引入所需要的包: import pandas as pd import numpy as n ...
- JavaScript正则表达式-字符
普通字符 大小写字母.数字.其他任何符号. 转义字符 转义字符 含义 \f 换页符 \n 换行符 \r 回车符 \t 制表符 \b 退格符 \o 空字符 \xnn 由十六进制数nn指定的ASCII码对 ...
- C#显示及隐藏任务栏
private const int SW_HIDE = 0; //隐藏任务栏 private const int SW_RESTORE = 9;//显示任务栏 [DllImport("use ...
- 读CSS DIV网页样式与布局心得体会
一.首先根据网页设计图拆分网页的整体结构 二.在html页面用DIV划分出结构块 三.再根据设计图在各个大<DIV>块中加入对应的小<DIV>块或者段落<P>,表单 ...
- tensorflow 如何限制显存大小
Python在用GPU跑模型的时候最好开多进程,因为很明显这种任务就是计算密集型的. 用进程池好管理,但是tensorflow默认情况会最大占用显存,尽管该任务并不需要这么多,因此我们可以设置显存的按 ...
- c语言头文件以及make注意事项
c语言头文件以及make注意事项 头文件说明:自己定义的头文件和项目文件放在一起,注意使用""而不是使用<>,系统的头文件才使用<> 当main函数要调用其 ...
- java面向抽象编程样例
import java.util.*; abstract class Geometry{ public abstract double getArea(); } class Pillar ...
- 【Luogu】P2680运输计划(树上差分+二分)
题目链接 总体思路……怎么说呢……是个暴力吧…… 首先用倍增预处理出每条路径的长度. 然后按长度把路径排序. 然后二分答案.对于当前答案mid检验,怎么检验呢? 首先差分把所有长度比mid大的链上除了 ...