Skip to content

Commit bb82b98

Browse files
authored
Merge pull request eugenp#6033 from er-han/BAEL-2470
[BAEL-2470] Added @value for map examples
2 parents 801d74a + 9003705 commit bb82b98

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

spring-core/src/main/java/com/baeldung/value/ValuesApp.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Arrays;
44
import java.util.List;
5+
import java.util.Map;
56

67
import javax.annotation.PostConstruct;
78

@@ -45,6 +46,27 @@ public class ValuesApp {
4546

4647
@Value("#{'${listOfValues}'.split(',')}")
4748
private List<String> valuesList;
49+
50+
@Value("#{${valuesMap}}")
51+
private Map<String, Integer> valuesMap;
52+
53+
@Value("#{${valuesMap}.key1}")
54+
private Integer valuesMapKey1;
55+
56+
@Value("#{${valuesMap}['unknownKey']}")
57+
private Integer unknownMapKey;
58+
59+
@Value("#{${unknownMap : {key1:'1', key2 : '2'}}}")
60+
private Map<String, Integer> unknownMap;
61+
62+
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
63+
private Integer unknownMapKeyWithDefaultValue;
64+
65+
@Value("#{${valuesMap}.?[value>'1']}")
66+
private Map<String, Integer> valuesMapFiltered;
67+
68+
@Value("#{systemProperties}")
69+
private Map<String, String> systemPropertiesMap;
4870

4971
public static void main(String[] args) {
5072
System.setProperty("systemValue", "Some system parameter value");
@@ -69,5 +91,12 @@ public void afterInitialize() {
6991
System.out.println(spelSomeDefault);
7092
System.out.println(someBeanValue);
7193
System.out.println(valuesList);
94+
System.out.println(valuesMap);
95+
System.out.println(valuesMapKey1);
96+
System.out.println(unknownMapKey);
97+
System.out.println(unknownMap);
98+
System.out.println(unknownMapKeyWithDefaultValue);
99+
System.out.println(valuesMapFiltered);
100+
System.out.println(systemPropertiesMap);
72101
}
73102
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
value.from.file=Value got from the file
22
priority=Properties file
3-
listOfValues=A,B,C
3+
listOfValues=A,B,C
4+
valuesMap={key1:'1', key2 : '2', key3 : '3'}

0 commit comments

Comments
 (0)