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

from:http://fanshuyao.iteye.com/blog/2267243

一、计划任务实现类

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

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

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

 

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

 

 

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

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

 

Xml代码   收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:task="http://www.springframework.org/schema/task"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  7.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  8.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         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>

Xml代码   收藏代码
  1. <context:component-scan base-package="com.spring.*">  
  2.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3.     </context:component-scan>  

 

 

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

Xml代码   收藏代码
  1. <task:annotation-driven scheduler="taskScheduler" mode="proxy"/>    
  2.     <task:scheduler id="taskScheduler" pool-size="10"/>    

 

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

 

Xml代码   收藏代码
  1. <task:annotation-driven />    

 

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

 

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

Java代码   收藏代码
  1. @Autowired  
  2. private PushRecordService pushRecordService;  

 记得set get方法

Java代码   收藏代码
  1. public PushRecordService getPushRecordService() {  
  2.         return pushRecordService;  
  3.     }  
  4.   
  5.     public void setPushRecordService(PushRecordService pushRecordService) {  
  6.         this.pushRecordService = pushRecordService;  
  7.     }  

 

 七,注意

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

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

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值