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
The post Spring-Boot appication.properties file location interview question: Part 1 appeared first on kode12.