@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注入失败的更多相关文章

  1. @Value取值为NULL的解决方案------https://blog.csdn.net/zzmlake/article/details/54946346

    @Value取值为NULL的解决方案 https://blog.csdn.net/zzmlake/article/details/54946346

  2. 【Intellij idea】spring中@Autowired注入失败

    @Autowired注入失败失败的解决办法? 现有的解决的方案是: 打开file-settings或者ctrl+alt+s -> Editor 然后在Inspections 点击搜索栏 输入Sp ...

  3. spring boot开发 @autowired注入失败

    @autowired注入失败 会出现如下错误提示: 2018-05-28 08:39:41.857 INFO 8080 --- [ restartedMain] org.hibernate.Versi ...

  4. @Value取值为NULL的解决方案

    在spring mvc架构中,如果希望在程序中直接使用properties中定义的配置值,通常使用一下方式来获取: @Value("${tag}") private String ...

  5. ajax post请求request.getParameter("")取值为null

    今天在写提交一个json数据到后台,然后后台返回一个json数据类型.但是发现后台通过request.getParamter("")取到的值为null. 于是写一个简单的ajax ...

  6. nacos作为配置中心动态刷新@RefreshScope添加后取值为null的一个问题

    之前springboot项目常量类如下形式: @Component @RefreshScope//nacos配置中心时添加上 public class Constants { @Value(" ...

  7. Spring Autowired 注入失败总是Null

    报错:NullPointerException 分析:错误原因是注入失败? <context:annotation-config/> <context:component-scan ...

  8. 当spring 对象@Autowired 注入失败或者创建对象Bean失败、No qualifying bean/Error creating bean 的失败情形分析和解决方案

    错误信息 今天开发的过程中突然出现如下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: N ...

  9. SpringBoot整合axis1.4后,@Autowired注入失败,使用工具类注入

    问题描述: 费劲心思搭建好webservices服务端后,没想到客户端调用失败,查看日志文件,发现报空指针异常,debug代码后,发现sql查询的值都是null.通常情况下,我们将Dao注入Servi ...

随机推荐

  1. php.ini文件修改完重启

    killall php-pfm启动php-pfm 一般 service php-fpm restart

  2. Linux中VIM的使用

    转自:http://www.lupaworld.com/?uid-296380-action-viewspace-itemid-118973 vi/vim 基本使用方法本文介绍了vi (vim)的基本 ...

  3. Bootstrap table方法,Bootstrap table事件,配置

    调用 BootStrap Table 方法的语法: $('#table').bootstrapTable('method', parameter); 例如: $('#my_table').bootst ...

  4. 【HDFS API编程】jUnit封装-改写创建文件夹

    首先:什么是jUnit  回顾: https://www.cnblogs.com/Liuyt-61/p/10374732.html 上一节我们知道: /** * 使用Java API操作HDFS文件系 ...

  5. LeetCode 206. Reverse Linked List倒置链表 C++

    Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...

  6. JAVA Freemarker + Word 模板 生成 Word 文档 (普通的变量替换,数据的循环,表格数据的循环,以及图片的东替换)

    1,最近有个需求,动态生成 Word 文当并供前端下载,网上找了一下,发现基本都是用 word 生成 xml 然后用模板替换变量的方式 1.1,这种方式虽然可行,但是生成的 xml 是在是太乱了,整理 ...

  7. azkaban使用--指定executor

    PS:局限,虽然可以指定在一台节点上执行,但是失去了高可用的优势,如果有不同类型的任务建议用多套azkaban 假如二台executor,下图中的4,5,我想将任务运行在hadoop02上,因为只有这 ...

  8. FLV 格式导入 视频

  9. chrome浏览器 新建 标签 页面 跳转到主页(或跳转到谷歌)

    我的浏览器是跳转到自己设置的主页.我在贴吧看到的以下方式,然后就可以了: 地址栏输入 chrome://flags 搜索 Enable doodles on the local NTP 这一项改为di ...

  10. 6993: Dominoes(纯bfs)

    题目描述Orz likes to play dominoes. Now giving an n*m chessboard and k dominoes whose size are 1*2, Orz ...