博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring quartz job autowired 出错 null pointer
阅读量:4314 次
发布时间:2019-06-06

本文共 1525 字,大约阅读时间需要 5 分钟。

什么情况下才能用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
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.

转载于:https://www.cnblogs.com/hong2016/p/7444076.html

你可能感兴趣的文章
小常识
查看>>
使用vscode开发python
查看>>
《java编程思想》读书笔记(一)开篇&第五章(1)
查看>>
swift--调用系统单例实现打电话
查看>>
0038-算一算是一年中的第几天
查看>>
51nod 1094 【水题】
查看>>
虚拟机设置静态IP地址
查看>>
Springboot上传文件出现MultipartException
查看>>
NHibernate错误:Could not compile the mapping document的解决
查看>>
关于vue的源码调试
查看>>
003.第一个动画:绘制直线
查看>>
K2BPM怎么让金融数据更有意义?
查看>>
史玉柱自述:我是如何带队伍的
查看>>
靶形数独【贪心+深搜】
查看>>
读大道至简第三章有感
查看>>
BeforeFieldInit的小叙
查看>>
TeamViewer的下载地址,低调低调
查看>>
005 线程ID和线程的优先级
查看>>
POJ 3067 Japan (树状数组 && 控制变量)
查看>>
python基础条件和循环
查看>>