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 application
only. 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.
- Spring Data CrudRepository saveAll() and findAll().
- Spring Data JPA CrudRepository count() Example.
- Spring Data JPA JpaRepository saveAndFlush() Example.
- Spring Data JPA CrudRepository delete() and deleteAll()
- Spring Data JPA CrudRepository deleteById() Example
- CrudRepository findAllById() Example Using Spring Boot
- Spring Data CrudRepository save() Method.
- Sorting in Spring Data JPA using Spring Boot.
- Spring Data JPA example using spring boot.
- Spring Data JPA and its benefit.
Other Spring Data JPA and Hibernate tutorials.
- @Version Annotation Example In Hibernate.
- Hibernate Validator Constraints Example Using Spring Boot.
- @Temporal Annotation Example In Hibernate/Jpa Using Spring Boot.
- Hibernate Table Per Concrete Class Spring Boot.
- Hibernate Table Per Subclass Inheritance Spring Boot.
- Hibernate Single Table Inheritance using Spring Boot.
- One To One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle.
- One To One Bidirectional Mapping Example In Hibernate/JPA Using Spring Boot and Oracle.
- One To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.
- Many To One Unidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And