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

server.port=8080
spring.application.name=MyApplication

and 2 more properties file available in application.yml file

server.port=9090
my.custom.property=MY_CUSTOM_PROPERTY_VALUE

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

server.port=8080 //from application.properties
spring.application.name=MyApplication //from application.properties
my.custom.property=MY_CUSTOM_PROPERTY_VALUE //from application.yml

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BlogApplication implements CommandLineRunner {
	
	@Value("${server.port}")
	String port ;

	@Value("${spring.application.name}")
	String appName;

	@Value("${my.custom.property}")
	String myCustomProp;

	public static void main(String[] args) {
		SpringApplication.run(BlogApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		System.out.println("Port: " + port);
		System.out.println("App Name: " + appName);
		System.out.println("Custom Prop: " + myCustomProp);
	}
}
Output
Port: 8080
App Name: MyApplication
Custom Prop: MY_CUSTOM_PROPERTY_VALUE

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

Thanks,
Yogesh Prajapati