spring-boot application.properties and yml property/file precedence: Interview Experience

Guys,

Again sharing one more spring boot interview experience.

This is a part 2 of our previous blog we have explained 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 ?

In this blog, we will explain one more combination of configuration file and properties.

There are 2 questions which confuse you if you haven’t tried it yet related multiple properties and yml files at one place and priority about properties available in those files.

Question:

  • Which file will spring-boot pick if you have application.properties and application.yml available in src/main/resources directory?
  • And what all properties are available to use and what all are ignored?

Answer:

If you have either application.properties or application.yml file available than spring will pick whatever available.
Now, what happens if both are available?
If both files are available than spring will give priority to application.properties and use all properties which are defined by it, and also spring will scan application.yml and use all properties which are not available in application.properties file.

Example:
let’s say we have 2 properties available in application.properties file

and 2 more properties file available in application.yml file

In this case, you have 3 unique properties available to use.

and server.port=9090 will be ignored as it already there in application.properties file.

It is better to check all combination of the configuration file and properties defined inside it.

Sample code to check combinations

Output

Hope, this will help to answer the interview question :)
Happy learning.

Thanks,
Yogesh Prajapati