日期:2014-05-19  浏览次数:20745 次

困扰好久了,spring quartz一个问题,job无法得到spring注入的属性bean,请CSDN年XDJM看一下,帮忙看一下,给分不是问题,万分感谢
事情是这样的,动态启动多个任务去发送消息,这个任务我是从数据库里获取,各个任务的启动是同一个JOB,只是每个任务JOB传入的参数不同,但这个JOB,需要一个spring 的dao,得不到spring注入的dao属性,不知道怎么回事

1.spring配置dao

--application*.xml--
<bean id="sendMsg" class="com.test.SendMsg">
  <property name="msgDao"><ref bean="msgDao"/></property>
</bean>

....msgDao已经配好数据源 spring
2.启任务
1)public calss TestInit(){
  //get tasklist
  public void initJobTrigger() throws Exception
  {
  List list=getTasklist();
  if(null!=list&&list.size()>0){
  Iterator ite=list.iterator();//这个可以得到dao 
  while(ite.hasNext()){
  Tmsg msg=(Tmsg)ite.next();
  JobDetail jobdetail=new JobDetail("msgJob_"+msg.getId(),Scheduler.DEFAULT_GROUP,SendMsg.class);
  SimpleTrigger sm=new SimpleTrigger("trigger_"+msg.getId(),Scheduler.DEFAULT_GROUP,new Date(),null,SimpleTrigger.REPEAT_INDEFINITELY,60L*10000L);
  try{
  scheduler.schedulerJob(jobDetail,sm);
  }catch(Exception ex){

  }
  scheduler.unscheduleJob("InitTrigger",Scheduler.DEFAULT_GROUP);//初始化任务只执行一行,执行一次之后移除初始化触发器
  scheduler.start();
  }
  }
}

public class SendMsg implements Job
{
  private MsgDao msgDao;
  //msgDao get,set方法,略
  ...
  public void execute(JobExecutionContext je) throws JobExecutionException{

  ......略
  List list=msgDao.getNeedMsg() ;//问题出在这里没法获得 msgDao

  .....do sth.  
  }
}

现在主要出现两个问题:

1.实现的job没法得到spring已经注入的dao,出现空指针,先前第一次初始化任务可以得到dao,但是动态生成任务,实现job,无法得到spring注入的属性dao,这是什么原因,请各位XDJM看有什么办法,困扰好久了...万分感谢...
2..多任务创建存在job和group id已经存在的错误,因为这些id是从数据库获取

------解决方案--------------------
你读取spring配置文件啦吗?ApplicationContext……
------解决方案--------------------
<property name="schedulerContextAsMap">
<map>
<!-- spring 管理的service需要放到这里,才能够注入成功 -->
<description>schedulerContextAsMap</description>
<entry key="xxService或xxRepo" value-ref="xxService或xxRepo" />
</map>
</property>
------解决方案--------------------
XML code

 <!--  定时执行任务的类,要继承TimerTask  --> 
   
  <!--  <bean  id ="SmsSendTask"  class ="com.aisino.platform.sms.core.SmsSendTask" /> -->  
     <!--  用Spring管理这个TimerTask,设定触发间隔  --> 
      <!-- 
      <bean  id ="ScheduledUserTimerTask"  class ="org.springframework.scheduling.timer.ScheduledTimerTask" > 
        <property  name ="delay" > 
          <value > 10000 </value > 
        </property > 
        <property  name ="period" > 
          <value > 60000 </value > 
        </property > 
        <property  name ="timerTask" > 
          <ref  local ="SmsSendTask" /> 
        </property > 
      </bean > 
     -->
    
    
    <!-- 
     <bean  id ="SmsQuickQueueSendTask"  class ="com.aisino.platform.sms.queue.SmsQuickQueueSendTask" />  
       <bean  id =&q