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 这几年,前端技术更新换代速度之快,每一年"最火的前端技术"排行榜都会换一番场景,本当に信じかねる.是"只闻新人笑不见旧人哭",还是 ...
随机推荐
- C#学习笔记一:C#开发环境的设置
C#是.NET Framework的一部分,用于编写.NET应用程序. C#集成开发环境(IDE) 微软提供了以下C#编程开发工具: Visual Studio 2010 (VS) Visual C# ...
- Multiplication Puzzle
题目大致意思是:一个整数序列包含N个1~100的整数(3<=N<=100),从中取出一个数并和相邻两边的整数相乘,依次进行下去直到只剩下首尾两个数为止,求最终的得到的和的最小值.两边的数不 ...
- PCB快速打样规范
基本情况 板材为FR-4,板厚1.6mm 板材铜厚为1/2oz,成品铜厚为1oz(加工过程中的沉铜工艺会让铜层增加厚度) 绿油白字 喷锡工艺 最小孔内铜厚1.27um 电铜18 ...
- Android Training精要(六)如何防止Bitmap对象出现OOM
1.使用AsyncTask異步加載bitmap圖片避免OOM: class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> ...
- AD认证
这两天接触到一个新的知识点,AD验证.什么是AD验证?Active Directory——活动目录,活动目录只是LDAP的一个实现,提供LDAP认证.Radius认证和NTML认证,都是标准认证方式 ...
- URPF技术白皮书
URPF技术白皮书 摘 要:本文介绍了URPF的应用背景,URPF主要用于防止基于源地址欺骗的网络攻击行为,例如基于源地址欺骗的DoS攻击和DDoS攻击:随后介绍了URPF的技术原理以及URPF ...
- oracle 10g WMSYS.WM_CONCAT 函數的用法
select t.rank, t.Name from t_menu_item t; 10 CLARK 10 KING 10 MILLER 20 ADAMS 20 FORD 20 JONES 20 SC ...
- 计算几何(凸包模板):HDU 1392 Surround the Trees
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So a ...
- webbrowser控件事件
Beforenavigate2: Fired before navigate occurs in the given WebBrowser(window or frameset element). T ...
- 《C语言程序设计现代方法》第2章 C语言基本概念
C语言的基本概念 第一个C程序例子. /* pun.c */ #include <stdio.h> int main(void) { printf("To C, or not t ...