简体   繁体   中英

Override spring-boot application.properties with external properties

I have a spring-boot application. I have 3 properties file:

  1. a property file inside the spring-boot jar - myjar.jar called application.properties ( which is package inside the jar )

  2. a property file outside the jar, at the jar location under configurations/global.properties

  3. a property file outside the jar, at the jar location under configurations/java.properties

The problem is, I'm running the following command:

java -Dlogging.config=logback.xml -jar "myjar.jar" spring.config.location=classpath:/application.properties,file:./configurations/global.properties,file:./configurations/java.properties

And I get an exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'Data.StoresDb.LakeConnectionString' in value "${Data.StoresDb.LakeConnectionString}"
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
        at org.springframework.beans.fa

in myApplication.java, I have:

@Value("${Data.StoresDb.LakeConnectionString}")
String dataLakeStoreDb;

and in my application.properties I do not have Data.StoresDb.LakeConnectionString, but I do have it in my global.properties, I would expect spring to resolve all files before trying to ser the value Data.StoresDb.LakeConnectionString

You pass the arg/value as a raw Java argument :

java -jar "myjar.jar" spring.config.location=...

The argument will be present in the String[] args of the main class but the Spring environment will not be aware about that.
You have to prefix it with -- to make the argument to be Spring environment aware :

java -jar "myjar.jar" --spring.config.location=...

From the official documentation :

By default SpringApplication will convert any command line option arguments (starting with “--”, eg --server.port=9000) to a property and add it to the Spring Environment

Or as alternative pass it as a system property :

java -Dspring.config.location=... -jar "myjar.jar" 

As a side note : beware spring.config.location overrides the default locations while spring.config.additional-location introduced in Spring Boot 2 adds the specified locations to the default locations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM