问题
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