A Quick Guide to Spring @Value
Further reading:
Using Spring @Value with Defaults
A quick and practical guide to setting default values when using the @Value annotation in Spring.
2. Setting up the Application
To describe different kinds of usage for this annotation, we need to configure a simple Spring application configuration class.
And naturally, we’ll need a properties file to define the values we want to inject with the @Value annotation. And so, we’ll first need to define a @PropertySource in our configuration class – with the properties file name.
Let’s define the properties file:
value.from.file=Value got from the file
priority=Properties file
listOfValues=A,B,C
3. Usage Examples
As a basic and mostly useless usage example we can only inject “string value” from the annotation to the field:
@Value("string value")
private String stringValue;
Using the @PropertySource annotation allows us to work with values from properties files with the @Value annotation. In the following example we get “Value got from the file” assigned to the field:
@Value("${value.from.file}")
private String valueFromFile;
We can also set the value from system properties with the same syntax. Let’s assume that we have defined a system property named systemValue and look at the following sample:
@Value("${systemValue}")
private String systemValue;
Default values can be provided for properties that might not be defined. In this example the value “some default” will be injected:
@Value("${unknown.param:some default}")
private String someDefault;
If the same property is defined as a system property and in the properties file, then the system property would be applied.
Suppose we had a property priority defined as a system property with the value “System property” and defined as something else in the properties file. In the following code the value would be “System property”:
@Value("${priority}")
private String prioritySystemProperty;
Sometimes we need to inject a bunch of values. It would be convenient to define them as comma-separated values for the single property in the properties file or as a system property and to inject into an array. In the first section, we defined comma-separated values in the listOfValues of the properties file, so in the following example the array values would be [“A”, “B”, “C”]:
@Value("${listOfValues}")
private String[] valuesArray;
4. Advanced Examples with SpEL
We can also use SpEL expressions to get the value. If we have a system property named priority, then its value will be applied to the field in the next example:
@Value("#{systemProperties['priority']}")
private String spelValue;
If we have not defined the system property, then the null value will be assigned. To prevent this, we can provide a default value in the SpEL expression. In the following example, we get “some default” value for the field if the system property is not defined:
@Value("#{systemProperties['unknown'] ?: 'some default'}")
private String spelSomeDefault;
Furthermore, we can use a field value from other beans. Suppose we have a bean named someBean with a field someValue equal to 10. Then 10 will be assigned to the field in this example:
@Value("#{someBean.someValue}")
private Integer someBeanValue;
We can manipulate properties to get a List of values. In the following sample, we get a list of string values A, B, and C:
@Value("#{'${listOfValues}'.split(',')}")
private List<String> valuesList;
5. Using @Value with Maps
First, we’ll need to define the property in the \{key: ‘value’ } form in our properties file:
valuesMap={key1: '1', key2: '2', key3: '3'}
Note that the values in the Map must be in single quotes.
Now we can inject this value from the property file as a Map:
@Value("#{${valuesMap}}")
private Map<String, Integer> valuesMap;
If we need to get the value of a specific key in the Map, all we have to do is add the key’s name in the expression:
@Value("#{${valuesMap}.key1}")
private Integer valuesMapKey1;
If we’re not sure whether the Map contains a certain key, we should choose a safer expression that will not throw an exception but set the value to null when the key is not found:
@Value("#{${valuesMap}['unknownKey']}")
private Integer unknownMapKey;
We can also set default values for the properties or keys that might not exist:
@Value("#{${unknownMap : {key1: '1', key2: '2'}}}")
private Map<String, Integer> unknownMap;
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
private Integer unknownMapKeyWithDefaultValue;
Map entries can also be filtered before injection. Let’s assume we need to get only those entries whose values are greater than one:
@Value("#{${valuesMap}.?[value>'1']}")
private Map<String, Integer> valuesMapFiltered;
We can also use the @Value annotation to inject all current system properties:
@Value("#{systemProperties}")
private Map<String, String> systemPropertiesMap;
6. Conclusion
In this quick tutorial, we examined the various possibilities of using the @Value annotation with simple properties defined in the file, with system properties, and with properties calculated with SpEL expressions.
As always the example application is available on GitHub project.