@value Reads the list, map, and string in configuration file YML

Here’s a simple example:

  • Application. Yml configuration file

server:
  port: 8096

# list
list1: a,b,c

# set
set1: a,b,c,a,b

# map
The map must be enclosed in quotes, the value must be enclosed, and the key must not be enclosed
map1: '{"name":"gavin","age":"18"}'
# map1: '{name:"gavin",age:"19"}'
Copy the code
  • ListConfig configuration class
@Configuration
@Data
public class ListConfig {

    @Value("#{'${list1}'.split(',')}")
    private List<String> list1;

    /** * The default value followed by a colon (:) indicates that if the yML file is not configured with list2, the default list2 is empty. List2 =[] */ is returned
    @Value("#{'${list2:}'.split(',')}")
    private List<String> list2;

    If the yML file is not configured with list2, the default list2 is empty. * If the list2 bit is empty, list3=null is returned. Otherwise, */ is returned
    @Value("#{'${list2:}'.empty ? null:'${list2:}'.split(',')}")
    private List<String> list3;

    If the yML file is not configured with list4, the default list4 is g. List4 =[g] */ is returned
    @Value("#{'${list2:g}'.split(',')}")
    private List<String> list4;

    If address is not configured in the YML file, the default value is ASD. Address = ASD */ is returned
    @Value("${address:asd}")
    private String address;

    /** * set1=[a, b, c] */
    @Value("#{'${set1}'.split(',')}")
    private Set<String> set1;


    @Value("#{${map1}}")
    private Map<String,String> map1;
}
Copy the code
  • Controller test file
RestController
@RequestMapping("/hello")
public class controller {

    @Autowired
    private ListConfig listConfig;

    @GetMapping("/test1")
    public void test(a) {
        List<String> test=listConfig.getList1();
        System.out.println("list1: "+test); //list1: [a, b, c]
    }
    @GetMapping("/test2")
    public void test2(a) {
        List<String> test=listConfig.getList2();
        System.out.println("list2: "+test); //list2: [] 
    }

    @GetMapping("/test3")
    public void test3(a) {
        List<String> test=listConfig.getList3();
        System.out.println("list3: "+test);  // list3: null
    }
    @GetMapping("/test4")
    public void test4(a) {
        List<String> test=listConfig.getList4();
        System.out.println("list4: "+test);  // list4: [g]
    }

    @GetMapping("/test5")
    public void test5(a) {
        String test=listConfig.getAddress();
        System.out.println("address: "+test);  // address: asd
    }

    @GetMapping("/test6")
    public void test6(a) {
        Set<String> test=listConfig.getSet1();
        System.out.println("set1: "+test);  // set1: [a, b, c]
    }
    
	// key = name, value = gavin
	// key = age, value = 18
    @GetMapping("/test7")
    public void test7(a) {
        Map<String,String> map=listConfig.getMap1();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key = " + entry.getKey() + ", value = "+ entry.getValue()); }}}Copy the code

The difference between single and double quotation marks in YML

“” : double quotation marks; Does not escape special characters inside the string; Special characters act as their intended meaning

information1: "Gavin \n Age 18"Output Information1: Gavin age18
Copy the code

“: single quotation mark; Special characters are escaped, and the special characters end up just plain string data

information2: 'Gavin \n Age 18'Output Information2: Gavin \n age18
Copy the code