Loading Configuration from Properties file
Spring Boot offers a flexible approach to managing properties from various sources. By default, it loads the properties from the application.properties
file. It also considers the OS environment variables, Java System properties and command-line arguments.
The resources are loaded in the following order of priority:
application.properties
located outside the packaged applicationapplication.properties
packaged within the application- OS environment variables
- Java system properties
- Command-line arguments
In the following example, we will create a weather service to load properties from the application.properties
file.
- Add the following properties inside
application.properties
file.
weather.api.key=default-api-key weather.default.city=New York weather.units=imperial
- Let us create a
WeatherService
class and load the properties fromapplication.properties
file
@Service public class WeatherService { @Value("${weather.api.key}") private String apiKey; @Value("${weather.default.city}") private String defaultCity; @Value("${weather.units}") private String units; public String getWeatherForecast() { return String.format("Weather forecast for %s: Sunny, 25°%s. API Key: %s", defaultCity, units.equals("metric") ? "C":"F", apiKey); } }
The @Value
annotation will instruct Spring to detect and load the value of that property.
In this example, it will detect the property weather.api.key}
inside the application.properties
file and load the value to the variable apiKey
.
We can define a default value to the @Value
annotation by adding a semicolon. Eg. @Value("${weather.api.key:defaultKey}")
. In this case, it will use the unit as defaultKey
when there are no properties defined with the key weather.api.key
.
If there are no properties defined inside the application.properties
file, it will though following exception.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'weatherService': Injection of autowired dependencies failed at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:515) ~[spring-beans-6.1.13.jar:6.1.13] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1439) ~[spring-beans-6.1.13.jar:6.1.13] ... omitted Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'weather.api.key' in value "${weather.api.key}"
Loading Configuration from YAML files
Spring boot also natively supports YAML files for managing configuration properties. YAML files use the same principle as .properties
files.
The application.properties file content can be written using YAML as:
application.yaml
weather: api: key: default-api-key default: city: New York units: imperial
Loading Configuration from External application.properties file
To load properties from an external file we need to run the application as a fat JAR. For that, we first need to build the application and launch the application with the java -jar
command.
Running as a fat JAR.
java -jar hello-spring-0.0.1-SNAPSHOT.jar
It will run the executable jar file and load the configuration from the application.properties
file if there is a file exists in the same location as the jar file.
Loading properties based on profile
Spring boot also allows you to isolate the environment-specific configurations using profiles. For example, if you want to use different configurations for dev, QA and production servers, you can create 3 different files (eg. application-dev.properties
, application-qa.properties
and application-prod.properties
) to have different configurations for each environment.
The profiles to be activated can be specified using the spring.profiles.active
property.
The profile-specific application-{profile}.properties
profiles take precedence over the non-profile-specific ones.
When profile-specific properties are defined, the resources are loaded in the following order of priority:
application-{profile}.properties
outside the packaged applicationapplication.properties
located outside the packaged applicationapplication-{profile}.properties
packaged inside the applicationapplication.properties
packaged within the application- OS environment variables
- Java system properties
- Command-line arguments
Override Configuration Using Command-Line Arguments
To override configuration using the command-line arguments, you can simply add --key=value
argument to your java -jar command.
java -jar hello-spring-0.0.1-SNAPSHOT.jar --weather.api.key=my-key
Note that, the arguments from the command line always have the highest precedence and override all other configurations.