Spring-Boot appication.properties file location interview question: Part 1
Guys,
Here i wanted to discuss one of the tricky interview quetion related to location of application.properties
file for spring-boot and microservice interview.
The question is
“Which file will be used by spring boot if one file(application.properties
) is present at your src/main/resources
path inside the jar and one more file present parallel to your jar file in file system ?”
Here are some tips for properties file usage in spring boot.
Spring boot will auto detected the application.properties
file from src/main/resources
and we can use all properties from that file.
Below is our sample application.properties
file.
application.properties
name=Vishal
Below is my DemoController.java class which access my application.properties
file value.
DemoController.java
@RestController public class DemoController { @Value("${name}") String name; @RequestMapping("/hello") public String sayHello() { return" Hello " + name; } }
Once you run the project and hit the below url in browser, the output ill be Hello Vishal.
http://localhost:8080/hello
output:
Hello Vishal
So spring will access the application.properties
from src/main/resources
folder.
Now generate the jar file using maven command mvn clen install.
Afte generating jar copy the jar file into another folder and create another application.properties
file parallel to copied jar file.
Below is my folder structure where my jar
file and another application.properties
file are available at same place.
Below is my new application.properties
with updated property value.
application.properties
name=Yogesh
Here in my jar file contains application.properties
file and the same folder where my “jar” file present i have another application.properties
file.
So let’s run the project in command prompt using below command.
java -jar PropertyFileTest-0.0.1-SNAPSHOT.jar
Now again run the projet and hit the below url in browser, the output ill be Hello Yogesh.
http://localhost:8080/hello
output:
Hello Yogesh
So here spring boot is accessing application.properties
which is available in same folder where our “jar” file available.
means spring is ignoring the application.properties
file which is available at src/main/resource
.
Hope, this will help to answer the interview question
Happy learning.
Thanks,
Vishal Ranapariya
Great thank you for the information.
I have created 2 applications, USING spring boot
– but cannot use them BOTH SIMULTANEOUSLY on my machine as both will work on port 8080.
So can the above be used to circumvent the same-port issue.
thanks
You have to specify a port in application.properties using property server.port=
Or
You can provide a port while running
App 1: product.jar
Command: java -jar product.jar -Dserver.port=8080 (App will run on 8080 )
App 2: order.jar
Command: java -jar order.jar -Dserver.port=8081 (App will run on 8081)
What about property file “meshing”? I mean suppose the property file in the jar defines `status=happy` what will the output of `return” Hello ” + name + “. I am ” + status` be?
If you have same status=<some text> in other property file which is available in same folder where your jar file then it’s override the status value, if not then print status=happy which is available in the jar.
If you have same status= <some text> in other property file which is available in same folder where your jar file then it’s override the status value, if not then print status = happy which is available in the jar.