Spring @Value取值为null或@Autowired注入失败
@Value 用于注入.properties文件中定义的内容
@Autowired 用于装配bean
用法都很简单,很直接,但是稍不注意就会出错。下面就来说说我遇到的问题。
前两天在项目中遇到了一个问题,大致描述就是我写了如下一个类(只列出关键代码):

@Component
@PropertySource("classpath:/config/config.properties")
public class MqttServiceClient implements IMqttServiceClient{
@Value("${mqtt_host}")
private String host;
@Value("${mqtt_client}")
private String client; private MqttClient mqttClient; public MqttService(){
mqttClient= new MqttClient(host,client, new MemoryPersistence());
}
}

并在其它类中定义了MqttServiceClient 类型的成员变量mqttServiceClient ,并用@Autowired注解进行注入,如下:
public class A{
@Autowired
private MqttServiceClient mqttServiceClient ;
...
}
然后运行时发现host和client总是为null,就是没有注入。但是各个配置都是对的,在项目中的其它类用同样的方法使用@Value注解时,就能够取到值。
提示的报错信息是MqttServiceClient 这个bean不能正确的创建。
一开始我以为是因为@Value注解没有正确取值导致MqttServiceClient 类的构造函数不能正常运行从而导致的错误,所以没在意提示的错误。
后来才发现,我所有的用法都没错,错就错在我重写的构造函数上:
因为Java类会先执行构造方法,然后再给注解了@Value的成员变量注入值(@Autowired也一样),所以在执行构造方法的时候,就会报错。
我的解决办法是,把@Value注解的属性写入一个AppConfig类中,然后在MqttServiceClient 中注入AppConfig类的对象,并用构造器注入,这样可以明确成员变量的加载顺序。
如下:

public class MqttServiceClient implements IMqttServiceClient{
private AppConfig appConfig;
private MqttClient mqttClient;
@Autowired
public MqttServiceClient(AppConfig appConfig){
this.appConfig = appConfig;
mqttClient = new MqttClient (appConfig.getHost(), appconfig.getClient, new MemoryPersistence()
}
}

Spring @Value取值为null或@Autowired注入失败的更多相关文章
- @Value取值为NULL的解决方案------https://blog.csdn.net/zzmlake/article/details/54946346
@Value取值为NULL的解决方案 https://blog.csdn.net/zzmlake/article/details/54946346
- 【Intellij idea】spring中@Autowired注入失败
@Autowired注入失败失败的解决办法? 现有的解决的方案是: 打开file-settings或者ctrl+alt+s -> Editor 然后在Inspections 点击搜索栏 输入Sp ...
- spring boot开发 @autowired注入失败
@autowired注入失败 会出现如下错误提示: 2018-05-28 08:39:41.857 INFO 8080 --- [ restartedMain] org.hibernate.Versi ...
- @Value取值为NULL的解决方案
在spring mvc架构中,如果希望在程序中直接使用properties中定义的配置值,通常使用一下方式来获取: @Value("${tag}") private String ...
- ajax post请求request.getParameter("")取值为null
今天在写提交一个json数据到后台,然后后台返回一个json数据类型.但是发现后台通过request.getParamter("")取到的值为null. 于是写一个简单的ajax ...
- nacos作为配置中心动态刷新@RefreshScope添加后取值为null的一个问题
之前springboot项目常量类如下形式: @Component @RefreshScope//nacos配置中心时添加上 public class Constants { @Value(" ...
- Spring Autowired 注入失败总是Null
报错:NullPointerException 分析:错误原因是注入失败? <context:annotation-config/> <context:component-scan ...
- 当spring 对象@Autowired 注入失败或者创建对象Bean失败、No qualifying bean/Error creating bean 的失败情形分析和解决方案
错误信息 今天开发的过程中突然出现如下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: N ...
- SpringBoot整合axis1.4后,@Autowired注入失败,使用工具类注入
问题描述: 费劲心思搭建好webservices服务端后,没想到客户端调用失败,查看日志文件,发现报空指针异常,debug代码后,发现sql查询的值都是null.通常情况下,我们将Dao注入Servi ...
随机推荐
- php.ini文件修改完重启
killall php-pfm启动php-pfm 一般 service php-fpm restart
- hanlp使用自定义词典抽取关键词
1.在data/dictionary/custom/路径下新建文件 myDict.txt.,添加新的单词,单词,词性,词频.并删除当前文件夹下的bin文件, 2.在hanlp配置文件中的CustomD ...
- 29.python环境搭建
新的笔记本上搭建的python运行环境:1.首先现在下载python版本,我这里拿的是之前用的3.5老版本 2.双击安装(勾选 add Python3.5 to path) 3.检测是否安装成功 4. ...
- hdfs webhdfs 写文件(create file)
# _*_ coding=utf-8 _*_ import sys import os hosts = {} cmd1 = ''' curl -i -X PUT "http://%s:500 ...
- MAIL服务器搭建
一,邮件服务: 优 点 缺 点 应 用 sendmail 有点年代久远,稳定功能多 太过于臃肿,配置文件多且繁琐 6以前默认 postfix 优点更稳定,且交轻便 发布年限较短,市场占有率低 ...
- 在aspx中,如果要引用一个ID号,需要引用外层的ID号(内层的不行)
- 10Linux_firewalld-Linux就该这么学
firewalld: runtime:当前生效,重启后失效(默认) permanent:当前不生效,重启后永久生效. A:重启,B:执行firewall-cmd --reload 数据链路层:ipta ...
- node-服务器
原生: const http=require('http'); http.createServer((request,response)=>{ response.writeHead(200,{& ...
- 这就涉及到ABAQUS历史输出中各能量变量的意义
ABAQUS中,对于很多动态问题,尤其像高速冲击模拟中,对结果评价很重要的一点就是要保证模型能量守恒,这就涉及到ABAQUS历史输出中各能量变量的意义,下面最各简单整理: ALLAE:人工伪应变能,六 ...
- tensorflow 升级到1.9-rc0,tensorboard 报错:TypeError: GetNext() takes exactly 1 argument (2 given)
Exception in thread Reloader:Traceback (most recent call last): File "/usr/lib/python2.7/threa ...