Search code examples
springspring-bootconfiguration-files

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


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?


Solution

  • 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;
        }