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. Linux下MySQL5.7.18 yum方式从卸载到安装

    本文出处:http://www.cnblogs.com/wy123/p/6932166.html 折腾了大半天,看了想,想了看,总算是弄清楚yum安装的过程了,之前写过二进制包安装的,这里用yum安装 ...

  2. Python中的logging模块【转】https://www.cnblogs.com/yelin/p/6600325.html

    [转]https://www.cnblogs.com/yelin/p/6600325.html 基本用法 下面的代码展示了logging最基本的用法. 1 # -*- coding: utf-8 -* ...

  3. day43 多表查询和pymysql

    复习 增删改查全语法 # 增 insert into db1.t1(字段2, 字段1, ..., 字段n)|省略 values (值2, 值1, ..., 值n)|(值1, 值2, ..., 值n)[ ...

  4. Python学习—爬虫篇之破解ntml登陆问题

    之前帮公司爬取过内部的一个问题单网站,要求将每个问题单的下的附件下载下来.一开始的时候我就遇到一个破解登陆验证的大坑......      (╬ ̄皿 ̄)=○ 由于在公司使用的都是内网,代码和网站的描述 ...

  5. 小强学渲染之OpenGL的GPU管线

    GPU渲染流水线,是硬件真正体现渲染概念的操作过程,也是最终将图元画到2D屏幕上的阶段.GPU管线涵盖了渲染流程的 几何阶段 和 光栅化阶段,但对开发者而言,只有对顶点和片段着色器有可编程控制权,其他 ...

  6. SSM框架整合思想

    -------------------siwuxie095                                 SSM 框架整合思想         1.SSM 框架,即 SpringMV ...

  7. Linux基本的操作

    一.为什么我们要学习Linux 相信大部分人的PC端都是用Windows系统的,那我们为什么要学习Linux这个操作系统呢???Windows图形化界面做得这么好,日常基本使用的话,学习成本几乎为零. ...

  8. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  9. IntelliJ IDEA常用快捷键(Mac)

    Mac 键盘符号和修饰键说明 ⌘ ——> Command ⇧ ——> Shift ⌥ ——> Option ⌃ ——> Control ↩︎ ——> Return/Ent ...

  10. kubernetes namespace Terminating

    1.kubectl get namespace annoying-namespace-to-delete -o json > tmp.jsonthen edit tmp.json and rem ...