JAVA/SPRING

YAML 매핑하기. @Value

gracelove91 2019. 8. 16. 21:38

@Value

폴더구조

application.yml

property:
  test:
    name: property depth test
propertyTest: test
propertyTestList: a,b,c

이제 변수에 yml파일에 저장돼있는 값을 매핑시켜보자.

AutoconfigurationApplicationTests.java

    @Value("${property.test.name}")
    private String propertyTestName;

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

    @Value("${propertyTestList}")
    private List<String> propertyTestList;

    @Value("${propertyTestList}")
    private String[] propertyTestArray;

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

    @Test
    public void testValue(){
        log.info("==================================================");
        log.info(propertyTestName);
        log.info(propertyTest);
        log.info(propertyTestList.toString());
        log.info(Arrays.toString(propertyTestArray));
        log.info(propertyTestListSplit.toString());
    }

결과.

2019-08-16 21:32:32.930  INFO 1970 --- [    Test worker] c.e.d.AutoconfigurationApplicationTests  : property depth test
2019-08-16 21:32:32.930  INFO 1970 --- [    Test worker] c.e.d.AutoconfigurationApplicationTests  : test
2019-08-16 21:32:32.930  INFO 1970 --- [    Test worker] c.e.d.AutoconfigurationApplicationTests  : [a, b, c]
2019-08-16 21:32:32.930  INFO 1970 --- [    Test worker] c.e.d.AutoconfigurationApplicationTests  : [a, b, c]
2019-08-16 21:32:32.930  INFO 1970 --- [    Test worker] c.e.d.AutoconfigurationApplicationTests  : [a, b, c]