I have a spring-boot application. I have 3 properties file:
a property file inside the spring-boot jar - myjar.jar called application.properties (which is package inside the jar)
a property file outside the jar, at the jar location under configurations/global.properties
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 “--”, e.g. --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.