spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
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定时任务的更多相关文章
- 使用SpringMVC自带的@Scheduled完成定时任务
首先在xml配置文件头中添加以下几行: xmlns:task="http://www.springframework.org/schema/task" http://www.s ...
- spring boot: @EnableScheduling开启计划任务支持,@Scheduled计划任务声明
spring boot: @EnableScheduling开启计划任务支持, @Scheduled计划任务声明 package ch2.scheduler2; //日期转换方式 import jav ...
- Spring Boot实战笔记(七)-- Spring高级话题(计划任务)
一.计划任务 从Spring3.1开始,计划任务在Spring中的实现变得异常的简单.首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在执行计划任务的方法上注解@Sc ...
- spring quartz分布式任务计划
spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...
- Spring注解之 @EnableScheduling计划任务注解
要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持, 然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务 示例:计划任务执行类 在这个 ...
- spring的Scheduled(定时任务)和多线程
一.前言 在我们日常的开发中,经常用到数据同步的更新,这时我们采用的是spring的定时任务和java的多线程进行数据的更新,进行时实的服务调用. 二.实现思路 1.创建线程类 ...
- spring注解scheduled实现定时任务
只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...
- spring整合mybatis,springMVC的0配置文件方式
0配置文件的形式主要是采用spring3.0提供的@configuration注解和spring容器在启动的时候会加载实现了WebApplicationInitializer的类,并调用其onStar ...
- 《经久不衰的Spring框架:SpringMVC 统括》
前言:经久不衰的Spring 这几年,前端技术更新换代速度之快,每一年"最火的前端技术"排行榜都会换一番场景,本当に信じかねる.是"只闻新人笑不见旧人哭",还是 ...
随机推荐
- MySQL源码 数据结构hash
MySQL源码自定义了hash表,因为hash表具有O(1)的查询效率,所以,源码中大量使用了hash结构.下面就来看下hash表的定义: [源代码文件include/hash.h mysys/has ...
- bzoj1072
还是那句话s<=10 必然想到状压 题目唯一的难点在于怎么转移整除 整除即是mod d=0,我们用f[cur,j]表示选取状况为cur,余数为j的方案数 注意一个数a1a2a3…an (ai表示 ...
- NOI2003 文本编辑器editor
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1908 Solved: 738[Submit][Statu ...
- awk将普通文本转换成json文件
script1: #!/bin/bash #Date:-- #Author:eivll0m awk -F"\t" -vq='"' '{ a[$]=a[$]?a[$]:$ ...
- android报错——The import android.util cannot be resolved
Eclipse导入外部Android工程时,总会遇到The import android.util cannot be resolved 错误,解决方法如下: 首先检查project.properti ...
- HDOJ --1172
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- Qt学习之路(2)------Qt中的字符串类
QString QString的一些基本用法 basic.cpp #include <QTextStream> int main(void) { QTextStream out(stdou ...
- Shell脚本语言与编译型语言的差异
大体上,可以将程序设计语言可以分为两类:编译型语言和解释型语言. 编译型语言 很多传统的程序设计语言,例如Fortran.Ada.Pascal.C.C++和Java,都是编译型语言.这类语言需要预先将 ...
- fzu 2135 数字游戏 【水题】
Problem 2135 数字游戏 Accept: 253 Submit: 392Time Limit: 1000 mSec Memory Limit : 32768 KB Problem ...
- hashCode与equals详解
在工作中写业务类通常都会重写hashCode与equals方法,而这两个方法的区别与用途也常常被问道.平时也只是大概知道这二者的用途,今天闲下来,查阅资料加上自己的理 解,总结记录下. hashCod ...