内容简介:翻译自: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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Security Testing Cookbook
Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99
Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!