zoukankan      html  css  js  c++  java
  • spring quartz job autowired 出错 null pointer

    什么情况下才能用autowired?

    当当前类属于spring IOC容器管时候就可以,比如在applicationContext.xml里有定义

    就是说在spring上下文里能够找到

    但是比如quartz job是quartz自己实例化,所以当下面这么做会报null pointer

    package com.xmal.dms.quatz;
    
    import java.util.List;
    
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.context.support.SpringBeanAutowiringSupport;
    
    import com.xmal.dms.dao.mapper.AbandonMovingToNewBedMapper;
    import com.xmal.dms.pojo.OccupancyInfo;
    
    public class AbandonMovingToNewBed implements Job{
    	
    	@Autowired
    	AbandonMovingToNewBedMapper abandonMovingToNewBedMapper;
    	@Override
    	public void execute(JobExecutionContext context) throws JobExecutionException {
    		// TODO Auto-generated method stub
    		List<OccupancyInfo> OccupancyInfoList = abandonMovingToNewBedMapper.QueryOccupancyWhereStatusIsTiaoFang("调房");
    		System.out.println("OccupancyInfoList"+OccupancyInfoList);
    		
    	}
    
    }
    

    这时只要加一句

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

     List<OccupancyInfo> OccupancyInfoList = abandonMovingToNewBedMapper.QueryOccupancyWhereStatusIsTiaoFang("调房");

    就好了

     解释:

    https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html#processInjectionBasedOnCurrentContext-java.lang.Object-

    Process @Autowired injection for the given target object, based on the current web application context.

    Intended for use as a delegate.

  • 相关阅读:
    this is a test from windows live writer 11529
    RoR部署方案深度剖析 (转载自javaeye)
    Rails每周一题(六): Security Guide(上) (转载)
    让你大脑变冷静的28句英文
    REST on Rails之自定义路由
    REST on Rails之资源嵌套
    #### 高薪 ##招聘:。。。。。 待遇非常不错。
    JavaEye网站的RoR性能优化经验谈 (转载)
    网页打印的分页问题
    Ruby Metaclass详解
  • 原文地址:https://www.cnblogs.com/hong2016/p/7444076.html
Copyright © 2011-2022 走看看