spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年12月28日, PM 05:37:54 星期一

http://fanshuyao.iteye.com/

一、计划任务实现类

1、用@Component注解标识计划任务类,这样spring可以自动扫描

2、在方法中使用注解标识要执行的方法:@Scheduled(cron="*/30 * * * * *")

3、周期可以使用cron,或者fixedRate,fixedRate=1000*30表示30秒执行一次,cron请自行百度或看下面的代码的说明

@Component
public class SpringTask { /**
* cron表达式:* * * * * *(共6位,使用空格隔开,具体如下)
* cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
* 注意: 30 * * * * * 表示每分钟的第30秒执行,而(*斜杠30)表示每30秒执行
*
* */
@Scheduled(cron="*/30 * * * * *")
public void firstTask(){
System.out.println("==============it is first task!时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}

二、需要在spring.xml配置文件中加入命名空间(xmlns:task)

本项目采用了spring4.1,所以用的是4.1

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
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-4.1.xsd">

三、增加包的扫描,一般在spring.xml文件都会有

主要是这个:<context:component-scan base-package="com.spring.*"></context:component-scan>

<context:component-scan base-package="com.spring.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

四、在spring.xml文件中配置计划任务

<task:annotation-driven scheduler="taskScheduler" mode="proxy"/>
<task:scheduler id="taskScheduler" pool-size="10"/>

也可以简单点配置,如下:

<task:annotation-driven />  

 五、然后启动web项目,就会看到每30秒就打印信息出来。

六、如果需要用到service类,可以注入。

@Autowired
private PushRecordService pushRecordService;

记得set get方法

public PushRecordService getPushRecordService() {
return pushRecordService;
} public void setPushRecordService(PushRecordService pushRecordService) {
this.pushRecordService = pushRecordService;
}

七,注意

同时还要添加一个aopaliaance.jar,否则会报错:noClassDefoundError:org/aopalliance/aop/Advice

地址:http://maven.ibiblio.org/maven2/aopalliance/aopalliance/1.0/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年12月28日, PM 05:37:54 星期一

http://fanshuyao.iteye.com/

spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务的更多相关文章

  1. 使用SpringMVC自带的@Scheduled完成定时任务

    首先在xml配置文件头中添加以下几行: xmlns:task="http://www.springframework.org/schema/task"   http://www.s ...

  2. spring boot: @EnableScheduling开启计划任务支持,@Scheduled计划任务声明

    spring boot: @EnableScheduling开启计划任务支持, @Scheduled计划任务声明 package ch2.scheduler2; //日期转换方式 import jav ...

  3. Spring Boot实战笔记(七)-- Spring高级话题(计划任务)

    一.计划任务 从Spring3.1开始,计划任务在Spring中的实现变得异常的简单.首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在执行计划任务的方法上注解@Sc ...

  4. spring quartz分布式任务计划

    spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...

  5. Spring注解之 @EnableScheduling计划任务注解

    要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持, 然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务 示例:计划任务执行类 在这个 ...

  6. spring的Scheduled(定时任务)和多线程

    一.前言 在我们日常的开发中,经常用到数据同步的更新,这时我们采用的是spring的定时任务和java的多线程进行数据的更新,进行时实的服务调用. 二.实现思路            1.创建线程类 ...

  7. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  8. spring整合mybatis,springMVC的0配置文件方式

    0配置文件的形式主要是采用spring3.0提供的@configuration注解和spring容器在启动的时候会加载实现了WebApplicationInitializer的类,并调用其onStar ...

  9. 《经久不衰的Spring框架:SpringMVC 统括》

    前言:经久不衰的Spring 这几年,前端技术更新换代速度之快,每一年"最火的前端技术"排行榜都会换一番场景,本当に信じかねる.是"只闻新人笑不见旧人哭",还是 ...

随机推荐

  1. DHTMLX 前端框架 建立你的一个应用程序教程(二)--设置布局

    Layout控件的演示 Dhtmlx有很多的组建来组织网页的建设, 这篇主要介绍dhtmlxLayout . 下面图片中 布局将各个组件(1.Menu 2.Toolbar 3.Grid 4.Form ...

  2. nc 命令汇总

    转自: http://blog.chinaunix.net/uid-20068039-id-359170.html 1.远程拷贝文件从server1拷贝文件到server2上.需要先在server2上 ...

  3. redhat下升级gcc编译器

    在有网络的环境下,采用下载gcc源码进行编译的方式升级gcc版本,所以需要本身已有gcc编译器. 获取 gcc-4.9.2的包: wget http://gcc.skazkaforyou.com/re ...

  4. C# 利用BarcodeLib.dll生成条形码

    首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...

  5. 织梦内容管理系统(DedeCms) 小说模块insert注入漏洞

    漏洞版本: Dedecms 漏洞描述: DedeCms是免费的PHP网站内容管理系统. 织梦内容管理系统(DedeCms) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户 ...

  6. oracle 大表删除数据后,回收空间的问题。

    在oracle中由于表结构设计不合理或者需要清楚老数据的时候,经常需要对大表数据进行清理. 一般有一下几种方法: 1. 删除大部分数据,留下小部分数据.我们可以把需要保留的数据转移到别的表,然后再把大 ...

  7. 阮老师讲解TF-IDF算法

    TF-IDF与余弦相似性的应用(一):自动提取关键词   作者: 阮一峰 日期: 2013年3月15日 这个标题看上去好像很复杂,其实我要谈的是一个很简单的问题. 有一篇很长的文章,我要用计算机提取它 ...

  8. PrintWriter out = response.getWriter() 输出中文乱码问题

    HttpServletResponse response = ServletActionContext.getResponse();        response.setCharacterEncod ...

  9. 你所不知道的C++

    C++与C的不同 C++从诞生之初就号称和C是兼容的,正是这种兼容,使C++得以迅猛发展,然而也正是这种兼容,让C++背上了沉重的历史包袱.且不论其利弊,让我们来看看C++在兼容C的那部分中,与C语言 ...

  10. (转载)PHP 判断常量,变量和函数是否存在

    (转载)http://www.jb51.net/article/17881.htm 如果你看懂了上面一句话,那么接下来都是废话,PHP手册写的还是很全的.一句话就把我标题中的问题全部解决了. 还是举几 ...