The @Value
annotation is used in Spring to inject values from various sources(for example application.properties, application.yml or any custom files) into your application beans. This is mainly used to inject/read configuration values or properties from different resources.
Here’s an example that says how to use Spring @Value
annotation. Before that consider we have application.properties file in the resources folder.
@Service
public class MyService {
@Value("${myapp.property}")
private String myProperty;
// ...
}
In this example, the MyService.java
class has a private field myProperty
that is annotated with @Value("${myapp.property}")
. This tells Spring to inject the value of the myapp.property
property from a configuration source into the myProperty
field.
The ${myapp.property}
syntax is used to reference a property in a Spring configuration source, such as a .properties
file, a YAML file, or environment variables. When Spring sees this syntax, it will look for a property with the name myapp.property
and inject its value into the annotated field.
Let’s see different ways that we can use the @Value
annotation in Spring.
- Read values from a properties file
@Value("${myapp.property}")
private String myProperty;
- Injecting values from an environment variable
@Value("${MYAPP_PROPERTY}")
private String myProperty;
Here’s an example of how to inject values from an environment variable using @Value
in Spring Boot.
Define the environment variable in your system. For example, on Unix systems, you can set an environment variable using the following command.
export MY_VAR=my_value
In your Spring Boot application, add the @Value
annotation to a field or a constructor parameter to inject the value from the environment variable. For example :-
@RestController
public class MyController {
@Value("${MY_VAR}")
private String myVar;
@GetMapping("/my-var")
public String getMyVar() {
return "The value of MY_VAR is: " + myVar;
}
}
In this example, the @Value("${MY_VAR}")
annotation injects the value of the MY_VAR
environment variable into the myVar
field.
Run your Spring Boot application and access the /my-var
endpoint. The response should include the value of the MY_VAR
environment variable:
The value of MY_VAR is: my_value
- Providing a default value if the property is not found
@Value("${myapp.property:default-value}")
private String myProperty;
Injecting a list of values
Suppose we have a configuration property in your application.properties file called myapp.list-property
with a comma-separated list of values.
@Service
public class MyService {
@Value("${myapp.list-property}")
private List<String> myList;
// ...
}
In this example, the @Value
annotation is used to inject the value of myapp.list-property
into the myList
field of the MyService
class. The SpEL
expression "${myapp.list-property}"
specifies the property name to be read, and Spring automatically converts the comma-separated string to a List
of String
S.
Note – We can also use #{}
syntax for more complex expressions, such as splitting the string by a delimiter other than a comma or filtering certain values. Here’s an example of using the SpEL
#{}
syntax to split the list by semicolon delimiter
@Service
public class MyService {
@Value("#{'${myapp.list-property}'.split(';')}")
private List<String> myList;
// ...
}
In this above example, the SpEL expression “#{‘${myapp.list-property}’.split(‘;’)}” is used to split the list by semicolon delimiter instead of a comma. This will produce a list of values ["value1,value2", "value3"]
.
Reading Map using @Value annotation
Consider we have the below property in application.properties file
my.map.property={key1:value1, key2:value2, key3:value3}
In our Spring application, we need to define a map property and annotate it with @Value
.
@Value("#{${my.map.property}}")
private Map<String, String> myMapProperty;
In this example, the @Value
annotation is used to inject the value of my.map.property
into the myMapProperty
map. The #{}
syntax is used to indicate that the value should be interpreted as a SpEL (Spring Expression Language) expression. The ${}
syntax is used to indicate that the value should be read from the properties file.
Note that the property value is enclosed in curly braces {}
and uses a comma-separated key-value pair syntax to represent a map. The @Value
annotation can interpret this syntax and convert it into a Map<String, String>
object for use in your code.
That’s all about Spring Boot @Value annotation example.
Other Spring Boot examples.
- Spring restful web services example using spring boot.
- Spring Data JPA example using spring boot.
- Spring batch basic.
- Spring batch task scheduling example using spring boot.
- Spring transaction management basic.
- Spring transaction management example using spring boot.
- Spring security default authorization example using spring boot.
- Spring security inMemoryAuthentication and authorization example using spring boot.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestBody and @ResponseBody annotation example in Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @RequestHeader annotation example by using Spring Boot.
- @SpringBootApplication annotation example in Spring Boot
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @ComponentScan example in spring boot.
- Content negotiation example using Spring Boot.
- @Configuration annotation example using spring boot.