首页 > 解决方案 > 如何在 Spring Boot 应用程序中使用命令行参数读取自定义属性文件

问题描述

启动我的 Spring Boot 应用程序时,我想使用命令行参数设置自定义配置文件路径,例如:

java -jar some.jar -DConfigPath=conf/config.local(or conf/config.prod) 

那么如何读取这个文件来生成一个spring配置呢?我可以@PropertySource以某种“动态”方式使用注释吗?

标签: springspring-bootconfiguration-files

解决方案


好的,我发现注解@PropertySource 可以获取命令行参数注入的值

   @Configuration
   @ConfigurationProperties
   @PropertySource(value = "file:${ConfigPath}")
   public class MyConfig {
            @Getter
            @Value("${property_name}")
            private String myproperty;
    }

推荐阅读