---
title: "spring定时器用Annotation实现"
author: "simple1024"
date: "2011-06-29T14:54:21Z"
tags: []
categories: ["编程语言"]
slug: "spring-annotation"
draft: false
lastmod: "2011-06-29T14:54:21Z"
id: 1109411
url: "https://simple1024.iteye.com/blog/1109411"
---

spring定时器用Annotation实现

 

由于项目中需要定时更新一些数据，所以用到了spring的定时器，在使用Quartz过程中，遇到了一些麻烦，最终牵扯的错误太多：

1、我的一个Service类中需要加入定时执行即Service extends QuartzJobBean，但是Service类中使用@Autowired注入了属性：dao对象

2、在executeInternal方法执行过程中，dao对象一直为null。

3、尝试了各种办法，最终在配置文件中这样配置（这种配置网上多的是）

使用jobDataAsMap这个属性

&lt;bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean"&gt;  
        &lt;property name="jobClass"&gt;  
            &lt;value&gt;org.wapgame.service.charts.Service&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="jobDataAsMap"&gt;  
            &lt;map&gt;  
                  &lt;entry key="dao" value-ref="Dao"/&gt;  
                  &lt;entry key="test" value="30"/&gt;  
             &lt;/map&gt;  
        &lt;/property&gt;  
 &lt;/bean&gt;

4、executeInternal方法中使用其参数来获得值。

protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {  
    // TODO Auto-generated method stub  
    JobDataMap map=arg0.getMergedJobDataMap();  
    int test = Integer.parseInt(map.get("test").toString());  
    log.debug("test:"+test);  
    playerDao=(PlayerDao)map.get("playerDao");  
  }

这样就解决了注入的对象为null的问题。网上找了好多找不到解决的办法，最终还得靠自己。

5、解决是解决了，但是过程中我需要用到事务操作。随之带来了很大的麻烦，毕竟项目用的都是Annotaion，再加上自己在ApplicationContext.xml中配置，造成了很多麻烦和异常。

6、放弃，转Annotation

 

**通过 注解 来调度任务**  
1、  
AnnotationQuartz类：  
import org.springframework.scheduling.annotation.Scheduled;  
import org.springframework.stereotype.Component;

@Component  
public class AnnotationQuartz {  
  @Scheduled(cron="0,10,20,30,40,50 * * * * ?")   //需要注意@Scheduled这个注解，它可配置多个属性：cron\\fixedDelay\\fixedRate  
  public void test()  
  {  
    System.out.println("0.0");  
  }  
}

2、  
spring的ApplicationContext.xml中的配置：  
只需要加上  
&lt;!--  定时器开关  开始--&gt;  
    &lt;task:annotation-driven/&gt;  
&lt;!--  定时器开关  结束--&gt;

3、(如果是web应用，那么需要再web.xml中加入)  
&lt;listener&gt;  
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
&lt;/listener&gt;

**通过 配置文件 来调度任务**  
1、  
AnnotationQuartz类：  
import org.springframework.stereotype.Component;

@Component(如果你的项目使用的是注解而不是配置文件中写bean，那么需要加上@Component，确保这个类已经注入)  
public class AnnotationQuartz {  
  public void test()  
  {  
    System.out.println("0.0");  
  }  
}  
2、  
spring的ApplicationContext.xml中的配置：  
&lt;task:scheduled-tasks&gt;   
    &lt;task:scheduled ref="chartsService" method="test" cron="0 0,15,30,45 * * * ?"/&gt;   
&lt;/task:scheduled-tasks&gt;

**最后请注意** ：(ApplicationContext.xml)  
a)、需要在xmlns里面加入：  
xmlns:task="http://www.springframework.org/schema/task"

b)、在xsi:schemaLocation中加入  
http://www.springframework.org/schema/task  
http://www.springframework.org/schema/task/spring-task-3.0.xsd(别忘了最后的引号)

 

 

cron表达式去搜吧，好多，我搜到了一些，如下：

&lt;!--

一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下:   
              1.秒(0-59)   
              2.分钟(0-59)   
              3.小时(0-23)   
              4.月份中的是期(1-31)   
              5.月份(1-12或SUN-DEC)   
              6.星期中的日期(1-7或SUN-SAT)   
              7.年份(1970-2099)    
                 例子:   
                  0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点   
                  0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟   
                  30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时   
                  0 0 8-5 ? * MON-FRI 每个工作日的工作时间   
              - 区间   
              * 通配符   
              ? 你不想设置那个字段 

--&gt;