Skip to content

Commit 3bdd542

Browse files
committed
Added @value for map examples
1 parent de80339 commit 3bdd542

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ public class ValuesApp {
4545

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

4970
public static void main(String[] args) {
5071
System.setProperty("systemValue", "Some system parameter value");
@@ -69,5 +90,12 @@ public void afterInitialize() {
6990
System.out.println(spelSomeDefault);
7091
System.out.println(someBeanValue);
7192
System.out.println(valuesList);
93+
System.out.println(valuesMap);
94+
System.out.println(valuesMapKey1);
95+
System.out.println(unknownMapKey);
96+
System.out.println(unknownMap);
97+
System.out.println(unknownMapKeyWithDefaultValue);
98+
System.out.println(valuesMapFiltered);
99+
System.out.println(systemPropertiesMap);
72100
}
73101
}
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)