我面临这个问题:我有一个类,其 @Value 变量声明为静态,如图所示。当我的应用程序启动 @PostConstruct 执行访问此类的方法时,找到具有 null 值的变量。
如果我在 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");
}
}
}
最佳答案
为什么要声明它是公开的和静态的?
尝试做
@Value("${access.to.my.variable}")
private String variable;
这应该以这种方式在@PostConstruct中可用
关于java - @PostConstruct执行中@Value静态注解为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56560262/