Merge pull request #6033 from er-han/BAEL-2470

[BAEL-2470] Added @Value for map examples
This commit is contained in:
Tom Hombergs 2019-01-03 20:13:21 +01:00 committed by GitHub
commit bb82b98470
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.value;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -46,6 +47,27 @@ public class ValuesApp {
@Value("#{'${listOfValues}'.split(',')}") @Value("#{'${listOfValues}'.split(',')}")
private List<String> valuesList; private List<String> valuesList;
@Value("#{${valuesMap}}")
private Map<String, Integer> valuesMap;
@Value("#{${valuesMap}.key1}")
private Integer valuesMapKey1;
@Value("#{${valuesMap}['unknownKey']}")
private Integer unknownMapKey;
@Value("#{${unknownMap : {key1:'1', key2 : '2'}}}")
private Map<String, Integer> unknownMap;
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
private Integer unknownMapKeyWithDefaultValue;
@Value("#{${valuesMap}.?[value>'1']}")
private Map<String, Integer> valuesMapFiltered;
@Value("#{systemProperties}")
private Map<String, String> systemPropertiesMap;
public static void main(String[] args) { public static void main(String[] args) {
System.setProperty("systemValue", "Some system parameter value"); System.setProperty("systemValue", "Some system parameter value");
System.setProperty("priority", "System property"); System.setProperty("priority", "System property");
@ -69,5 +91,12 @@ public class ValuesApp {
System.out.println(spelSomeDefault); System.out.println(spelSomeDefault);
System.out.println(someBeanValue); System.out.println(someBeanValue);
System.out.println(valuesList); System.out.println(valuesList);
System.out.println(valuesMap);
System.out.println(valuesMapKey1);
System.out.println(unknownMapKey);
System.out.println(unknownMap);
System.out.println(unknownMapKeyWithDefaultValue);
System.out.println(valuesMapFiltered);
System.out.println(systemPropertiesMap);
} }
} }

View File

@ -1,3 +1,4 @@
value.from.file=Value got from the file value.from.file=Value got from the file
priority=Properties file priority=Properties file
listOfValues=A,B,C listOfValues=A,B,C
valuesMap={key1:'1', key2 : '2', key3 : '3'}