The @Value annotation in Spring Boot is useful for injecting external values into your application.
Introduction to @Value in Spring Boot
@Value annotation can inject values from property files, environment variables, system properties, and command line arguments. This annotation makes it easier to externalize your application configuration, allowing you to modify the external value without recompiling the code.
Here we are going to understand how to use @Value with property files. All you have to do is add the @Value annotation in your class attribute indicating the property you want to be read.
We will look at how to use the @Value annotation in SpringBoot. Take a look at the next examples about how to use @Value with your property file.
Inject @Value in attributes
We said that In Spring Boot, the @Value annotation can be used to inject values from a property file. To do so, first, create a properties file containing the values you want to inject.
For example, we can create this application.properties
that contains the following values:
app.example.integer-value=9876
app.example.decimal-value=1.75
app.example.boolean-value=true
Then, you can use the @Value annotation to inject these values into your Java class attributes.
You can include these fields in your Java class and annotate them with @Value to indicate the property name, as shown below:
@Value("${app.example.integer-value}")
private Integer valueInteger;
@Value("${app.example.decimal-value}")
private Double valueDouble;
@Value("${app.example.boolean-value}")
private Boolean valueBoolean;
When the application is launched, the fields with @Value will be set to the values from the application.properties file. This makes it simple to externalize your application configuration, allowing you to change the values without recompiling the code.
Note that Spring converts the property file value to the attribute type.
You should use this format ${propertyname}
. If you don’t use this format the value will be literal and not from the property value.
Inject @Value in Constructors
The @Value annotation can also be used in constructors to inject values when the class is constructed.
Here, we use the @Value annotation in a constructor to inject values from the application.properties
file.
The constructor takes two parameters and annotates them with @Value to indicate the property name, as illustrated as follows. The class should be a component, in this case, we use directly @Component. But could be any type of component stereotype such as a Service, Repository, Configuration.
@Component
public class AppComponent {
private final Integer valueInteger;
private final Double valueDouble;
public AppComponent(@Value("${app.example.integer-value}") Integer valueInteger,
@Value("${app.example.decimal-value}") Double valueDouble) {
this.valueInteger = valueInteger;
this.valueDouble = valueDouble;
}
public Double sum() {
return this.valueDouble + this.valueInteger;
}
}
@Value with Default Values
We can also use the @Value annotation to set a default value for a field if the property is not found in the properties file.
For example, we can set the default value for this field like this:
@Value("${app.example.nonexistent-value:'This is a default value''}")
private String defaultValue;
Here, we have set the default value to This is a default value
if the app.example.nonexistent-value
property is not found in the properties file. This can be useful to set a default value when the property is not present in the configuration.
Example @Value with Arrays Properties
The @Value annotation can also be used to inject values from an array of properties into your application. To do this, create a properties file containing the values of the array.
For example, we can make the following list of countries:
app.example.countries=Argentina, Brazil, Italy, Spain
Include a List<String>
field in your Java class and annotate it with @Value to indicate the property name, see this:
@Value("${app.example.countries}")
private List<String> countries;
You can also use this format to set a default value for an array of strings:
@Value("${app.example.countries:Argentina, Brazil, Italy, Spain}")
private List<String> countries;
The List<String> countries
array will be set to the default value of Argentina, Brazil, Italy, Spain
if the app.example.countries
property is not found in the properties file.
Using the @Value Annotation with Maps
The @Value annotation can also be used to inject values from a map defined with a single property into your application. To do this, create a properties file containing the values of the map.
This application.properties
file contains the following value that contains a map:
app.example.map-values=[keyA:'A', keyB:'B', keyC:'C']
Include a Map<String,String>
field in your Java class with @Value to indicate the property name.
Notice, the format changes → @Value("#{${app.example.map-values}}") . The name of this format is Spring Expression Language (SpEL).
@Value("#{${app.example.map-values}}")
private Map<String,String> mapValues;
Example @Value with Enums
The @Value annotation can also be used to inject values from an enum. Create a properties file containing the enum values.
For example, we can create this application.properties
file containing the following value that contains an enum:
app.example.enum-value=RED
Include an Enum
field in your Java class and annotate it with @Value to indicate the property name:
@Value("${app.example.enum-value}")
private ColorEnum color;
public enum ColorEnum {
RED,
GREEN,
BLUE
}
Example @Value with Local Date
The @Value annotation can also be used to inject values from a Local Date.
In this application.properties
the value contains a Local Date.
You have to use the ISO-8601 calendar system yyyy/dd/mm, such as 2025-12-01
app.example.local-date=2025-12-01
Your field with LocalDate
in your Java class with @Value:
@Value("${app.example.local-date}")
private LocalDate localDate;
Example @Value with LocalDateTime
The @Value annotation can also be used to inject values from a LocalDateTime.
In this application.properties
the value contains a LocalDateTime.
Use the ISO-8601 calendar system yyyy/dd/mm/hh:mm:ss, such as 2025-12-01T09:00:00
app.example.local-datetime=2025-12-01T09:00:00
Your field with LocalDateTime
in your Java class with @Value:
@Value("${app.example.local-datetime}")
private LocalDateTime localDateTime;
Running all of the previous examples
We have this property file.
app.example.integer-value=9876
app.example.decimal-value=1.75
app.example.boolean-value=true
# array
app.example.countries=Argentina, Brazil, Italy, Spain
# map
app.example.map-values={keyA:'A', keyB:'B', keyC:'C'}
# enum
app.example.enum-value=RED
# local date
app.example.local-date=2025-12-01
# local date time
app.example.local-datetime=2025-12-01T09:00:00
This component.
@Component
public class AppComponent {
private final Integer valueInteger;
private final Double valueDouble;
public AppComponent(@Value("${app.example.integer-value}") Integer valueInteger,
@Value("${app.example.decimal-value}") Double valueDouble) {
this.valueInteger = valueInteger;
this.valueDouble = valueDouble;
}
public Double sum() {
return this.valueDouble + this.valueInteger;
}
}
This Enum
public enum ColorEnum {
RED,
GREEN,
BLUE
}
And this Spring Main class
@SpringBootApplication
public class SpringBootValueAnnotationApplication {
@Value("${app.example.integer-value}")
private Integer valueInteger;
@Value("${app.example.decimal-value}")
private Double valueDouble;
@Value("${app.example.boolean-value}")
private Boolean valueBoolean;
@Value("${app.example.nonexistent-value:'This is a default value'}")
private String defaultValue;
@Value("${app.example.countries}")
private List<String> countries;
@Value("#{${app.example.map-values}}")
private Map<String, String> mapValues;
@Value("${app.example.enum-value}")
private ColorEnum color;
@Value("${app.example.local-date}")
private LocalDate localDate;
@Value("${app.example.local-datetime}")
private LocalDateTime localDateTime;
@Autowired
private AppComponent appComponent;
public static void main(String[] args) {
SpringApplication.run(SpringBootValueAnnotationApplication.class, args);
}
@PostConstruct
public void doExamples() {
System.out.println("-- Running examples --");
// attribute / field inject
System.out.println("-- attribute inject --");
System.out.println(valueInteger);
System.out.println(valueDouble);
System.out.println(valueBoolean);
// constructor inject
System.out.println("-- component inject --");
System.out.println(appComponent.sum());
//default value
System.out.println("-- default value --");
System.out.println(defaultValue);
// arrays
System.out.println("-- arrays --");
countries.forEach(System.out::println);
// map
System.out.println("-- map --");
mapValues.forEach((k, v) -> System.out.println((k + ":" + v)));
// Enum
System.out.println("-- enum --");
System.out.println(color);
// local dates
System.out.println("-- local date --");
System.out.println(localDate);
// local date time
System.out.println("-- local date time --");
System.out.println(localDateTime);
}
}
Running this main class we will see the following in the console:
-- Running examples --
-- attribute inject --
9876
1.75
true
-- component inject --
9877.75
-- default value --
'This is a default value'
-- arrays --
Argentina
Brazil
Italy
Spain
-- map --
keyA:A
keyB:B
keyC:C
-- enum --
RED
-- local date --
2025-12-01
-- local date time --
Conclusion
In this article, we have learned how to use the @Value annotation in Spring Boot to inject values from properties files into your application and how to use the @Value annotation to inject values into a Component class. We have seen how to inject values from simple properties (string, integer), as well as arrays, maps, enums, LocalDate, and LocalDateTime. Now we know that @Value annotation simplify access to all values of our property file.
This code is in GitHub