我的类(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/