java - yml 文件中的属性值未加载到 springboot 项目中的类中
问题描述
请在下面找到 application.yml 的代码
decrypt: /Users/Blahblah/Bleh
我们试图读入类的上述属性请找到 PropertyLoader.java 的代码
@Configuration
@Component
public class PropertyLoader implements InitializingBean{
@Value("${decrypt}")
private String decryptPath;
<--->
}
值 decryptPath 始终为空。谁能告诉我代码有什么问题?
解决方案
首先 application.yml 应该在src/main/resources/application.yml
.
如果你想在构造函数中使用这个变量,你不会。因为spring在构造之后注入@Value注释变量。但是如果你想在构造函数中做你可以这样做:
public class PropertyLoader implements InitializingBean{
private String decryptPath;
public PropertyLoader(@Value("${decrypt}") decrypPath) {
this.decryptPath = decryptPath;
}
}
推荐阅读
- c# - 在只读 docker 容器中运行 dotnet app
- python - macOS上的python opencv contrib全屏
- java - 表格调整大小并占用垂直滚动条留下的空间
- css - 将样式组件道具与下一个邻居选择器结合起来不起作用
- laravel - Laravel 使用规则对象进行自定义验证:访问其他字段或验证器
- docker - 为什么我无法从命令部分执行命令在 docker compose 中设置环境变量
- solidity - 为什么我不能将 USDT 转入我的智能合约?
- python - 使用 getitem 运行 Python 程序时出现关键错误
- sql - SQL Timeseries - 按非活动间隔对行进行分组(会话窗口)
- date - Power BI 计算按 DatesBetween 过滤的最大值