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

最新文章

  1. 慕课网-Java入门第一季-7-4 编程练习
  2. TCP中异常关闭链接的意义 异常关闭的情况
  3. localStorage和sessionStorage区别
  4. OGG配置
  5. 在线程中进行读取并写入文件和wenjia
  6. git 的记住用户名和密码和一些常用
  7. SiteMesh
  8. WPF中StackPanel的使用方法
  9. openstack私有云布署实践【1 网络拓扑说明】
  10. LINQ to Sql系列一 增,删,改
  11. [刷题]算法竞赛入门经典(第2版) 5-4/UVa10763 - Foreign Exchange
  12. python之format函数
  13. Python3学习之路~5.10 PyYAML模块
  14. 核态获取PsLoadedModuleList地址的稳定方法
  15. Android实现选择题答题(包括单选、多选和答题卡)
  16. sqlserver 镜像 断开连接 正在恢复+主机服务器关机用备用镜像
  17. UVALive 6910 Cutting Tree 并查集
  18. nodejs服务器部署教程四
  19. Mongodb操作之查询(循序渐进对比SQL语句)(转http://www.tuicool.com/articles/UzQj6rF)
  20. Mockito 相关资料

热门文章

  1. lintcode-100-删除排序数组中的重复数字
  2. lintcode-83-落单的数 II
  3. lintcode-74-第一个错误的代码版本
  4. [Java文件操作] 为文本文件添加行号
  5. BZOJ4487 JSOI2015染色问题(组合数学+容斥原理)
  6. P4016 负载平衡问题
  7. 2018牛客多校第一场 A.Monotonic Matrix
  8. Generator的基本用法
  9. NOIP2010 codevs1069 洛谷P1525 关押罪犯
  10. BZOJ 3262: 陌上花开 CDQ