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. DJango 基础 (5)

    模板加载静态文件 在settings.py文件中添加STATICFILES_DIRS,设置静态文件目录路径,同templates. # settings.py文件中​STATIC_URL = '/st ...

  2. windows 远程连接“发生身份验证错误 要求的函数不受支持”

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\P ...

  3. 安装Python-Jenkins

    有两种方式安装: ①有网络:sudo pip install python-jenkins ②无网络: 下载文件:https://pypi.org/project/python-jenkins/#fi ...

  4. python动态模块导入

    首先创建一个模块目录lib,然后在目录内创建一个模块为:aa.py 官方推荐: import importlib aa = importlib.import_module('lib.aa') c = ...

  5. HDU 5977 Garden of Eden(点分治求点对路径颜色数为K)

    Problem Description When God made the first man, he put him on a beautiful garden, the Garden of Ede ...

  6. 交叉编译sudo

    编译Sudo version 1.8.6p7下载路径:https://www.sudo.ws/news.html 1.交叉编译 # tar -xvf sudo-1.8.6p7.tar.gz # cd ...

  7. php Pthread 多线程 Worker

    <?php //PHP 高级编程之多线程 http://www.netkiller.cn/journal/thread.php.html#idp57489856 //worker 是一个具有持久 ...

  8. Legend 图例

    1.添加图例 >>> import matplotlib.pyplot as plt >>> import numpy as np >>> x = ...

  9. [JAVA]JAVA章3 如何获取及查看DUMP文件

    一.dump基本概念 在故障定位(尤其是out of memory)和性能分析的时候,经常会用到一些文件来帮助我们排除代码问题.这些文件记录了JVM运行期间的内存占用.线程执行等情况,这就是我们常说的 ...

  10. SPARK安装一:Windows下VirtualBox安装CentOS

    一.虚拟机安装 重点是网络设置,参见:https://www.linuxidc.com/Linux/2018-04/151924.htm 本文用三台2核4g虚拟机做集群,虚拟机安装centos7,如下 ...