1. Bean依赖关系

  一个配置类的Bean,一个实例Bean;

  实例Bean初始化时需要依赖配置类的Bean;

1.1 配置类Bean

@ConfigurationProperties(prefix = "system")
public class SystemConfig { private Integer type; private String rootPath;
}

1.2 实例Bean

@Component
public class HDFSFileHandler implements FileHandler { @Autowired
private SystemConfig config; private FileSystem fileSystem; public HDFSFileHandler(){
start();
} /**
* 初始化 fileSystem
*/
private void start() {
try {
// 此处 config 空指针异常
if (SystemConstant.FILE_SYSTEM_HDFS.equals(config.getType())){
String uri = "hdfs://" + config.getHdfsIp() + ":" + config.getHdfsPort();
fileSystem = FileSystem.get(new URI(uri), new Configuration(), "hadoop");
log.debug("uri:" + uri);
log.debug("fileSystem:" + fileSystem);
}
} catch (Exception e) {
log.error("init fileSystem occur a exception",e);
}
}
}

2. 问题现象

  实例Bean初始化时配置类Bean空指针异常;

  

3. 原因分析

  spring在实例化Bean时,先通过反射调用构造方法生成一个基本对象,然后再填充属性(参考:spring bean 的生命周期);

  填充属性之前属性值都为默认值,引用类为null,构造方法中使用属性对象时属性对象还未被设置,所以为null;

4. 解决方案

4.1 方案一

  构造器中将Bean作为参数显式的传入;

@Component
public class HDFSFileHandler implements FileHandler { private SystemConfig config; private FileSystem fileSystem;
  // 构造器显式传入参数
public HDFSFileHandler(SystemConfig config) {
this.config = config;
start();
} /**
* 初始化 fileSystem
*/
private void start() {
try { if (SystemConstant.FILE_SYSTEM_HDFS.equals(config.getType())){
String uri = "hdfs://" + config.getHdfsIp() + ":" + config.getHdfsPort();
fileSystem = FileSystem.get(new URI(uri), new Configuration(), "hadoop");
log.debug("uri:" + uri);
log.debug("fileSystem:" + fileSystem);
}
} catch (Exception e) {
log.error("init fileSystem occur a exception",e);
}
}
}

4.2  @Autowired + @PostConstruct

@Component
public class HDFSFileHandler implements FileHandler { @Autowired
private SystemConfig config; private FileSystem fileSystem; public HDFSFileHandler() {
start();
} /**
* 初始化 fileSystem
*/
@PostConstruct
private void start() {
try {
if (SystemConstant.FILE_SYSTEM_HDFS.equals(config.getType())){
String uri = "hdfs://" + config.getHdfsIp() + ":" + config.getHdfsPort();
fileSystem = FileSystem.get(new URI(uri), new Configuration(), "hadoop");
log.debug("uri:" + uri);
log.debug("fileSystem:" + fileSystem);
}
} catch (Exception e) {
log.error("init fileSystem occur a exception",e);
}
}
}

  

Spring 中初始化一个Bean对象时依赖其他Bean对象空指针异常的更多相关文章

  1. ZeroMQ接口函数之 :zmq_msg_init_data - 从一个指定的存储空间中初始化一个ZMQ消息对象的数据

    ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_init_data zmq_msg_init_data(3) ØMQ Manual - ØMQ/3.2.5 ...

  2. Spring中初始化bean和销毁bean的时候执行某个方法的详解

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过注解@PostConstruct  和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 ...

  3. spring中的控制反转IoC和依赖注入DI

    原文:http://blog.163.com/xianghuxian@126/blog/static/50639037200721345218382/ IoC(Inversion of Control ...

  4. Spring中为什么继承了ApplicationContextAware接口就可以使用ApplicationContext对象?

    1.Spring中使用applicationContext对象 public class SpringContextUtil implements ApplicationContextAware { ...

  5. Spring中的一个错误:使用Resources时报错(The annotation @Resources is disallowed for this location)

    在学习Spring的过程中遇到一个错误:在使用注解@resources的时候提示:The annotation @Resources is disallowed for this location 后 ...

  6. Spring中使用两种Aware接口自定义获取bean

    在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bean对象,这时候,就可以通过实现BeanFactoryAware来满足需求,代码很简单: @Servicepublic clas ...

  7. 关于在PHP中当一个请求未完成时,再发起另一个请求被阻塞的问题

    最近做项目的时候遇到个问题,就是做阿里云oss大文件上传进度条显示,因为要实时查询上传分片进度,所以在上传的同时必须要再发起查询的请求,但是一直都是所有分片上传完成后查询的请求才执行,刚开始以为是阿里 ...

  8. spring中RequestBody注解接收参数时用JSONField转参数名无效问题

    问题: 在springboot项目中使用@RequestBody注解接收post请求中body里的json参数的情况.即: @RequestMapping(value = "/get-use ...

  9. 总结:在MyEclipse中部署一个wap应用时需要配置的环境变量,我的JDK是安装在C盘,mysql安装在D盘,Tomcat解压在E盘,所以路径一定要看清楚哦,!

随机推荐

  1. 云笔记项目-补充JS面向对象编程基础知识

    简单介绍: 此部分知识为在做云笔记项目中补充,因为云笔记项目中涉及到前端js,里面写了很多js脚本,用到了创建js属性和方法,在js中直接声明的属性和方法最终都会变成window的对象,即其成为了全局 ...

  2. Cdnbest的cdn程序默认支持web Socket

    Cdnbest的cdn程序默认支持web Socket    WSS 是 Web Socket Secure 的简称, 它是 WebSocket 的加密版本. 我们知道 WebSocket 中的数据是 ...

  3. 深入浅出PF 学习笔记---资源文件

    引用   xmlns:sys="clr-namespace:System;assembly=mscorlib" <Window.Resources><sys:St ...

  4. Oracle修改表结构字段名和字段长度

    添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….); 修改字段的语法:alter ...

  5. Balanced Numbers (数位dp+三进制)

    SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...

  6. python note 02 格式化与判断、字符串转换

    1.格式化输出% %s %d name = input ('请输入姓名:') age = input ('请输入年龄:') height = input ('请输入身高:') msg = " ...

  7. vue js校验金钱、数字

    // 校验保留两位小数金额 export function isMoney(money) { var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1} ...

  8. etcd-v2第二集

    参考文章:https://github.com/coreos/etcd/blob/master/Documentation/v2/api.mdhttp://www.cnblogs.com/zhengr ...

  9. SSD性能测试第一神器:FIO

    SSD性能测试第一神器:FIO  2017年8月12日 syswift 0 对于SSD性能测试来说,最好的工具莫过于FIO了. 上面这个可爱的小伙子名字叫Jens Axboe,他是丹麦哥本哈根大学计算 ...

  10. RNA-seq差异表达基因分析之TopHat篇

    RNA-seq差异表达基因分析之TopHat篇 发表于2012 年 10 月 23 日 TopHat是基于Bowtie的将RNA-Seq数据mapping到参考基因组上,从而鉴定可变剪切(exon-e ...