java - 使用 @AllArgsConstructor 获取 @Value 的空值
问题描述
当我将 @Value 与 @AllArgsConstructor 组合使用时,我得到 null 作为值。
@Service
@Slf4j
@AllArgsConstructor
public class ReconService{
private ReconRepo reconRepo;
private InService inService;
@Value("${threshold.count}")
private Integer threshold;
public void doSomething(List<Record> records) {
if(threshold < records.size()) {
throw new RuntimeException("threshold exceeded");
}
}
}
在中doSomething()
,我将threshold
变量值设为 null 你能帮我解决这个问题吗?
解决方案
您可以使用@RequiredArgsConstructor 通过构造函数注入您的bean,并使用字段注入注入您的阈值。为此,您必须使您的豆子最终化
@Service
@Slf4j
@RequiredArgsConstructor
public class ReconService{
private final ReconRepo reconRepo;
private final InService inService;
@Value("${threshold.count}")
private Integer threshold;
public void doSomething(List<Record> records) {
if(threshold < records.size()) {
throw new RuntimeException("threshold exceeded");
}
}
}
推荐阅读
- pandas - 更改熊猫数据框的架构
- angular - 如何避免两次调用观察方法?
- python - 请求翻译时似乎未使用上传的词汇表
- c++ - 带有 C++ Builder 10.x 的 FASTMM 调试版本
- swift - 如何快速将日期和时间分别发送到后端?
- terraform - 我们可以跳过 terraform 资源中某些部分的执行吗
- sparql - SPARQL 限制特定变量的结果数
- apache-kafka - 特定主题的配置。卡夫卡猫
- c++ - 如何在 vscode 中看到 __vfptr?
- amazon-web-services - 在 AWS Lambda 中,线程限制是否适用于每个 lambda 实例