java – Spring @Value(“${}”)经常为null

栏目: Java · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/28636060/spring-value-often-null
我正在使用 Spring

Boot应用程序.在某些@Component类中,加载@Value字段,而不是在其他类上,它们始终为null.

似乎在创建@ Bean / @组件后加载了@Value.

我需要从@Bean中的属性文件加载一些值.

你有什么建议吗?

在构造bean(执行构造函数)之后注入属性(以及所有bean依赖项).

如果需要,可以使用构造函数注入.

@Component
public class SomeBean {
    private String prop;
    @Autowired
    public SomeBean(@Value("${some.prop}") String prop) {
        this.prop = prop;
        //use it here
    }
}

另一种选择是在使用@PostConstruct注释的方法中移动构造函数逻辑,它将在创建bean之后执行,并且所有它的依赖项和属性值都将被解析.

翻译自:https://stackoverflow.com/questions/28636060/spring-value-often-null


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

The Probabilistic Method

The Probabilistic Method

Noga Alon、Joel H. Spencer / Wiley-Interscience / 2008-8-11 / USD 137.00

Praise for the Second Edition : "Serious researchers in combinatorics or algorithm design will wish to read the book in its entirety...the book may also be enjoyed on a lighter level since the diffe......一起来看看 《The Probabilistic Method》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具