how to read custom property files with command line argument in a spring boot application

余生长醉 提交于 2020-01-05 13:33:53

问题


when startup my spring boot application, I want to set a custom config file path by using command line argument, for example:

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

So how to read this file to generate a spring configuration? Can I use the Annotation @PropertySource in some "dynamic" way?


回答1:


Ok, I find the annotation @PropertySource can get the value injected by command line argument

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



回答2:


Try using

spring.config.location

Something like this

java -jar some.jar --spring.config.location=file:some-project/src/main/resources/conf/config.prod

this should read your config just like any application.properties file



来源:https://stackoverflow.com/questions/50245650/how-to-read-custom-property-files-with-command-line-argument-in-a-spring-boot-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!