This post will discuss how to read a value defined in the application.properties file in a Spring Boot application.

In Spring Boot, we can use properties files, YAML files, environment variables, and command-line arguments to externalize our configuration. This is useful while working with the same application code in different environments. This post will discuss how to read a value defined in the properties files.

1. @Value annotation

The simplest way is to use the @Value annotation to load variables from the application.properties.

Suppose in application.properties, we have a property project.name. Then the property values can be injected directly into your beans by using the @Value annotation, as shown below:

 
Controller Code:

2. Spring’s Environment abstraction

The property values can also be accessed through Spring’s Environment abstraction, as shown in the following example:

 
Another alternative is to simply inject Environment into our controller/bean.

That’s all about reading values from the application.properties file in Spring Boot.