SpringMvc定时器任务

在最近的工作中,涉及到一个定时任务,由于以前对springMVC使用较少,所以,上网找了一点资料.这个demo感觉挺好,推荐给大家.

使用到的JAR文件:

aopalliance-1.0.jar
commons-logging-1.1.3.jar
spring-aop-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
spring-webmvc-3.2.4.RELEASE.jar

首先要配置我们的SpringMVC文件

xmlns 加下面的内容、

[xml] 预览复制

  1. xmlns:task="http://www.springframework.org/schema/task"

然后xsi:schemaLocation加下面的内容、

[xml] 预览复制

  1. http://www.springframework.org/schema/task
  2. http://www.springframework.org/schema/task/spring-task-3.2.xsd

最后是我们的task任务扫描注解

[xml] 预览复制

  1. <!-- task任务扫描注解 -->
  2. <task:annotation-driven/>

我配置的扫描位置是

[xml] 预览复制

  1. <context:component-scan base-package="com.wuzhut"></context:component-scan>

下面写出一个测试类

[java] 预览复制

  1. package com.wuzhut.task;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class MyTask {
  6. @Scheduled(cron="0/5 * * * * ? ") //间隔5秒执行
  7. public void taskCycle(){
  8. System.out.println("无主题(www.wuzhuti.cn) <span style="color: #000000;">专注于前端开发技术和程序开发研究的技术博客</span>");
  9. }
  10. }

注意

需要注意的几点:

1、spring的@Scheduled注解  需要写在实现上、

2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)

3、实现类上要有组件的注解@Component

送上demo源码供学习参考:http://pan.baidu.com/s/1mg0yxss

本文转自:http://wuzhuti.cn/850.html(本人支持原创)

时间: 2024-08-29 05:58:41

SpringMvc定时器任务的相关文章

Springmvc定时器

applicationContext-jobs.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:task=&qu

springmvc 定时器

CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明  是否必填  允许填写的值 允许的通配符 1  秒  是  0-59    , - * / 2  分  是  0-59   , - * / 3 小时  是  0-23   , - * / 4  日  是  1-31   , - * ? / L W 5  月  是  1-12 or JAN-DEC   , - * / 6  周  是  1-7 or SUN-SAT   , - * ? /

Spring定时器注解配置

spring-task.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:context="http://www.springf

springmvc之定时器

一.通过注解方式出发定时器 1.工程结构 2.所需jar包 3.spring-config.xml,springmvc配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta

SpringMVC使用Cron表达式的定时器

SpringMVC的功能很强大,集成了Quartz定时器的功能.能够通过Cron表达式和简单的注解就实现定时运行任务的功能. 网上看到不少样例,可是都不是非常全. 闲话少说.首先要在springmvc.xml中加入以下几行: xmlns:task="http://www.springframework.org/schema/task" <!--以下两行要放在xsi:schemaLocation里面--> http://www.springframework.org/sche

springMVC框架配置定时器

在springmvc.xml添加如下内容在xmlns中添加 xmlns:task="http://www.springframework.org/schema/task"1在xsi中添加 http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.2.xsd12在中间添加 <!-- 设置定时任务 --><task:annotatio

使用SpringMVC写一个定时器

一.首先xml头部增加几个标签 xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd" 二.开启扫描注解.(注意这个是一定要开启的.否则无效,有时还要注意如果只扫描Controller时的处理) <con

springMVC设置定时器定时执行某个方法

   <!--人事档案工龄 在applicationContext.xml中配置-->   <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->         <bean id="pstartQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.

SpringMVC自带的定时器,超好用,基于注解

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd 配置文件中加入这个 <!-- task任务扫描注解 --> <task:annotation-driven/> <context:component-scan base-package="com.*******.tog.*.time"><