Spring 中初始化一个Bean对象时依赖其他Bean对象空指针异常
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对象空指针异常的更多相关文章
- 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 ...
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过注解@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 ...
- spring中的控制反转IoC和依赖注入DI
原文:http://blog.163.com/xianghuxian@126/blog/static/50639037200721345218382/ IoC(Inversion of Control ...
- Spring中为什么继承了ApplicationContextAware接口就可以使用ApplicationContext对象?
1.Spring中使用applicationContext对象 public class SpringContextUtil implements ApplicationContextAware { ...
- Spring中的一个错误:使用Resources时报错(The annotation @Resources is disallowed for this location)
在学习Spring的过程中遇到一个错误:在使用注解@resources的时候提示:The annotation @Resources is disallowed for this location 后 ...
- Spring中使用两种Aware接口自定义获取bean
在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bean对象,这时候,就可以通过实现BeanFactoryAware来满足需求,代码很简单: @Servicepublic clas ...
- 关于在PHP中当一个请求未完成时,再发起另一个请求被阻塞的问题
最近做项目的时候遇到个问题,就是做阿里云oss大文件上传进度条显示,因为要实时查询上传分片进度,所以在上传的同时必须要再发起查询的请求,但是一直都是所有分片上传完成后查询的请求才执行,刚开始以为是阿里 ...
- spring中RequestBody注解接收参数时用JSONField转参数名无效问题
问题: 在springboot项目中使用@RequestBody注解接收post请求中body里的json参数的情况.即: @RequestMapping(value = "/get-use ...
- 总结:在MyEclipse中部署一个wap应用时需要配置的环境变量,我的JDK是安装在C盘,mysql安装在D盘,Tomcat解压在E盘,所以路径一定要看清楚哦,!
随机推荐
- 2019/1/9 6系列所有装置编号与SIM卡信息抓取
一.报错:File "G:/station/6系列装置信息明细.py", line 12, in <module>workbook.save(r'.\6系列装置明细表. ...
- 工艺CODE
- Kivy 从memory 读取image
借助PIL来处理的图片数据 fp = BytesIO() img = Image.frombytes('RGB', img_size, buf_bytes, 'raw', 'BGR;16', 0, 1 ...
- ASP 错误捕捉,处理
Asp利用 On Error Resume Next捕捉异常,根据Err.Number判断是否有错误 注:On Error Goto 0取消捕捉异常 模板文件页面 <% Response.Buf ...
- Eclipse 创建Android 模拟器失败:no cpu/abi system image available for this target
(从网上搜了一个使用Android 4.4 API 20编译的图片) 这是因为SDK中没有模拟器使用的操作系统镜像. 如果项目使用API 19编译,则SDK中的system-images文件夹下,需要 ...
- 关于php条形码生成(barcode),修改样式
今天听错了需求,以为要重新设计条形码,第一次制作这个,经过搜索使用的barcode这个第三方的,具体使用步骤网上很多就不在这里详细介绍了.主要是今天遇到的样式修改问题: barcode经过查看是无法自 ...
- 2017-2018-2 20165315 实验四《Android程序设计》实验报告
2017-2018-2 20165315 实验四<Android程序设计>实验报告 第24章:初识Android Android Studio项目的目录树 1 build:该目录包含了自动 ...
- 正则冷知识;分组捕获、replace()的用法...
1.var reg=/./; var reg=/\./的区别?? 前者代表任意一个字符,后者代表这个字符串中得有一个.. 2.?的使用?? 如果单独的一个字符串后面带? , var reg=/\d?/ ...
- javaweb开发.eclipse使用小常识
一.javabean快速生成 1.写好属性,如 package me.lst.domain; import java.io.Serializable;import java.sql.Date; /** ...
- day 6 元组、字典、字符串
本节内容 : 1,元组 2,字典 3,字符串作业 produce = [('mac', 9000), ('bicycle', 800), ('computer', 8000), ('book', 50 ...