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.

6
  • 1
    What is the actual question? Commented Oct 19, 2019 at 21:58
  • I want to get homeDir as the result rather than other results. Commented Oct 19, 2019 at 22:47
  • I have couple of question here, 1) in project root directory which is src ? 2) where are passing the value for this ${LOGNAME}` placeholder ? @new_programmer Commented Oct 20, 2019 at 0:44
  • @Deadpool: 1. Root directory outside src folder. 2. ${LOGNAME} is to replace with userID. so that the path form to /User/<userId> (which is the directory where my application.properties reside) Commented Oct 20, 2019 at 3:11
  • Which spring boot version are you using, @new_programmer ? Commented Oct 29, 2019 at 15:38

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.