gpt4 book ai didi

java - 无法读取一次性 bean 中的 @Value 注释值

转载 作者:行者123 更新时间:2023-11-29 07:36:45 24 4
gpt4 key购买 nike

我的类(class)看起来像

public class LevelDBStore implements DisposableBean {
@Value("${leveldb.maxOpenFiles:1000}")
private String maxOpenFilesValue;

public LevelDBStore(String storeName, long cacheSizeInMb, int maxOpenFiles,DBComparator dbComparator) {
storeLocation = new File(STORE_HOME, storeName);
db = init(storeLocation, cacheSizeInMb, maxOpenFiles, dbComparator);
isValid = true;
}

private DB init(File storeLocation, long cacheSizeInMb, int maxOpenFiles,
DBComparator dbComparator) {
logger.info("MaxOpenFilesValue=" + maxOpenFilesValue);
}
.....
}

当这段代码运行时,我得到

11 Jan 2016 14:33:14,325 [INFO ] [main] LevelDBStore         | MaxOpenFilesValue=null

这里有什么问题?我通常使用其他 bean,但不使用 DisposableBean

最佳答案

您正在从构造函数中调用 .init。 Spring 的注入(inject)机制在实际对象构造之后触发,因此构造函数无法获得注入(inject)的成员。

不要直接调用.init,而是在方法上加上@PostConstruct 注解。 Spring会在构建和注入(inject)完成后自动调用。

关于java - 无法读取一次性 bean 中的 @Value 注释值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34732423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com