spring - 如何在 Spring Boot 应用程序中使用命令行参数读取自定义属性文件
问题描述
启动我的 Spring Boot 应用程序时,我想使用命令行参数设置自定义配置文件路径,例如:
java -jar some.jar -DConfigPath=conf/config.local(or conf/config.prod)
那么如何读取这个文件来生成一个spring配置呢?我可以@PropertySource
以某种“动态”方式使用注释吗?
解决方案
好的,我发现注解@PropertySource 可以获取命令行参数注入的值
@Configuration
@ConfigurationProperties
@PropertySource(value = "file:${ConfigPath}")
public class MyConfig {
@Getter
@Value("${property_name}")
private String myproperty;
}
推荐阅读
- javascript - 如何在Javascript中正确过滤全名字符串?
- ruby-on-rails - 对由远程参数定义的结果进行排序
- android-recyclerview - FlexboxLayoutManager 项目在滚动另一个 Recycler 视图时闪烁
- kotlin - 在我的选项卡式应用程序上寻找正确的语法来设置 setmaxlifecycle
- python - 动态地将数据附加到熊猫数据框
- c# - 匹配词汇单词和短语
- python - y = x / sum(x, dim=0) 的反向传播,其中张量 x 的大小为 (H,W)
- c# - 在应用程序配置文件中找不到名为“BACEntities”的连接字符串。” 当从 .NET 5 Api 调用 'n .NET Framwork 4.7.2 Library
- python-3.x - Numpy更改3D数组中的对角线值
- ios - 迁移到 SwiftUI 应用程序流时启动时出现黑屏