How Spring Boot loads properties or yml or yaml file

Did you ever notice Spring boot automatically reads the configuration file if we define the configuration file with the name application.properties or application.yml? In this post, we will see How Spring Boot loads properties or yml or yaml file. If we define these files with some different name Spring Boot will not load. In that case, we need to use some other annotation to load the custom configuration file.

In ConfigFileApplicationListener.java it has been defined as a constant which tells about the default location where these configurations should be kept.

class ConfigFileApplicationListener {

private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";

private static final String DEFAULT_NAMES = "application";

}

The following combination is allowed as the default file name.

It appends .properties or .yml or yaml extension later.

Let’s see list of files which loads default by Spring Boot.

application.properties
application.yml
application.yaml
application.xml

applcation.properties

We get how it read from some specific location. why we should keep properties file in some location. But the file name is applicationonly. It should be something called applicatin.properties or application.yml.

Default locations allow reading application.properties file automatically?

Let’s see the default directory locations that are used to keep the application.properties file.

The default location for the properties file.

“classpath:/,classpath:/config/,file:./,file:./config/”

If you put application.properties in the resources folder then it will be considered in classpath:/application.properties.

There are two predefined classes PropertiesPropertySourceLoader and YamlPropertySourceLoader in org.springframework.boot.env package that implements the PropertySourceLoader interface.

public interface PropertySourceLoader {

	List<PropertySource<?>> load(String name, Resource resource) throws IOException;

}

Can we use both application.properties and appication.yml at the same time?

No. We should not. We may get unexpected result.

We can override the default file name, using @PropertyResource annotation, and define a custom configuration file.

That’s all about How Spring Boot loads properties or yml or yaml file example.

You may like.

Other Spring Data JPA and Hibernate tutorials.