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 这几年,前端技术更新换代速度之快,每一年"最火的前端技术"排行榜都会换一番场景,本当に信じかねる.是"只闻新人笑不见旧人哭",还是 ...
随机推荐
- QT事件研究的文章
我始终认为,Windows里最重要的不是API,而是消息.同理,QT里最重要的是事件.然而我对事件的原理和用法至今不是很理解,先放几篇文章在这里,有空回来细细研读: http://m.blog.chi ...
- PHP数组和Json之间的互相转换 json_encode() 和 json_decode()
之所以要用到Json,很多时候是因为使用ajax对象时,程序与JS函数之间的数据交互.因为JS不认识PHP中的数组,PHP也不认识JS中的数组或对象.Json很好的解决了这个问题. Json简介 JS ...
- ajax 初始化请求前携带参数
$(function () { function SetAjax(wxOpenId, departCode) { $.ajaxSetup({ xhrF ...
- 使用GDI+轻松创建缩略图
Gdi+ 还是相当好用的. 1> Image保存图像,需要一个CLSID的参数,它可以这样获得: int GetEncoderClsid(const WCHAR* format, ...
- SQL Server 2008 远程过程调用失败[ VS2012]
查看SQL Server配置管理工具,SQL Server 2008 服务中提示“远程过程调用失败”. [解决办法] 首先,核查用户名密码是否正确. 其次,查看sql server 配置管理器中,第一 ...
- ARM学习笔记12——GNU ARM汇编伪操作
1..section 1.1.语法格式 .section section_name[,"flags"[,%type[,flag_specific_arguments]]] 1.2. ...
- ldr指令总结
LDR/STR字和无符号字节加载/存储 1,LDR Rd,[Rn] 2, LDR Rd,[Rn,Flexoffset] 3, LDR Rd,[Rn],Flexoffset 4, LDR Rd,la ...
- 主流智能手机屏幕材质介绍 及 LCD闪屏现象分析
TN TN(Twisted Nematic) 即扭曲向列型面板,属于有源矩阵液晶显示器中的一种.由于TFT是主动式矩阵LCD可让液晶的排列方式具有记忆性,不会在电流消失后马上恢复 ...
- bayer图像格式
Bayer数据,其一般格式为:奇数扫描行输出 RGRG……偶数扫描行输出 GBGB…… 根据人眼对彩色的响应带宽不高的大面积着色特点,每个像素没有必要同时输出3种颜色.因此,数据采样时, 奇数扫描行的 ...
- Tornado基本使用
一.快速上手 #!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web class M ...