@Value 注入Map list等类型

时间:2025-02-13 21:05:15

在写入下面代码

=true
=abc
=123
=123
=1.2345678123456
=1.2345678123456
=1,3,4,5,6,1,2,3
=1,3,4,5,6,1,2,3
=1,3,4,5,6,1,2,3
={name:"张三", age:18}

使用junit进行测试

@RunWith()
@SpringBootTest(classes = {})
public class DemoTest {

    @Value("${}")
    private Boolean testBoolean;

    @Value("${}")
    private String testString;

    @Value("${}")
    private Integer testInteger;

    @Value("${}")
    private Long testLong;

    @Value("${}")
    private Float testFloat;

    @Value("${}")
    private Double testDouble;

    @Value("#{'${}'.split(',')}")
    private String[] testArray;

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

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


    @Value("#{${}}")
    private Map<String, Object> testMap;

    @Test
    public void demoTest(){
        (testBoolean); // true
        (testString); // abc
        (testInteger); // 123
        (testLong); // 123
        (testFloat); // 1.2345678
        (testDouble); // 1.2345678123456

        ((testArray)); 
        // ["1","3","4","5","6","1","2","3"]

        (()); 
        // [1, 3, 4, 5, 6, 1, 2, 3]

        (()); 
        // [1, 3, 4, 5, 6, 2]

        (()); 
        // {name=张三, age=18}
    }

}