【发布时间】:2019-10-26 20:29:04
【问题描述】:
我正面临这个问题:我有一个类,其 @Value 变量声明为静态,如图所示。当我的应用程序启动对此类方法的@PostConstruct 执行访问时,找到具有空值的变量。
如果我在 de @PostConstruct 执行后调用这个类,我可以看到 @Value 静态变量不为 null 并且它具有正确的值。
如何在@PostConstruct 执行中获得这个变量的真实值?
这是我的代码:
@Component
public class ValidacionesUtils {
public static String variable;
@Value("${access.to.my.variable}")
public void setVariable(String variable) {
this.variable = variable;
}
@PostConstruct
private void init() {
if (variable==null) {
log.error("This is what my application prints");
} else {
log.error("This is not printed");
}
}
}
【问题讨论】:
-
尝试将您的设置器的主体更改为
ValidacionesUtils.variable = variable -
通过构造函数传入。
-
它不起作用@michalk
标签: java spring-boot static postconstruct