0

I am learning spring boot config externalization. I would like my application to read configuration from any external file (/Users/<userid>/application.properties).

I created spring-web project, and added spring.config.location=${user.home} entry to src/main/resource/application.properties

testConfig is the configuration property name I am trying to access via below code in my Controller class.

 @Value("${testConfig})
 private String configValue;

I have given different values for testConfig in different places.

testConfig=InsideJar in src/main/resources/application.properties file. testConfig=ApplicationRoot in project root directory application.properties file.
testConfig=homeDir in user home directory /User/<userid>/application.properties file.

How I ran the app and what is the result?

1. From different directory than project root  

Command: java -jar ../Downloads/first-springboot-app/target/first-springboot-app-0.0.1-SNAPSHOT.jar -Dspring.config.location=file:/User/${LOGNAME}/application.properties Result:InsideJar Expected result : homeDir

.

2. From Project root directory    

Command: java -jar target/first-springboot-app-0.0.1-SNAPSHOT.jar -Dspring.config.location=file:/Users/${LOGNAME}/application.properties

Result:applicationRootDirExpected result : homeDir

3. Added spring.config.name=application
4. Added spring.config.name=application.properties

Both of the above (3,4) gave same result .
Result:applicationRootDir Expected result : homeDir.

I already went through below links and other blogs .
Springboot doc
SO, SO.

Question/Problem : Why value (homeDir) is not fetched from Users/<userid>/application.properties ?
Any help would highly appreciated.

new_programmer
  • 840
  • 3
  • 12
  • 22

1 Answers1

0

This should definitely work as expected, even without adding the spring.config.name.

I have ran a simple spring application with only web dependency from start.spring.io and added -Dspring.config.location=file:/Users/my-user/application.properties. Works as expected.

I then tested again with -Dspring.config.location=file:/Users/${MY_USER}/application.properties and declared MY_USER=my-user, which also worked. I did this to test that the an environment variable is also read from the path itself.

That being said, you are having an issue with the config and are probably missing some small setting. I have posted my code here so feel free to check it out and compare it with yours.

Hope that helps,

Cheers

Urosh T.
  • 3,336
  • 5
  • 34
  • 42