기본 값
app:
el:
demo:
default: default text
comma: el1,el2,el3,el4
mapv: "{key1:'1',key2:'2'}"
각 코드
주의사항으로는 모두 *.properties 와 동일하지만 Map 처리의 경우 중괄호 밖을 한번 더 감싸야 한다.
properties => app.el.demo.mapv = {key1:'1',key2:'2'}
yml => 위 코드 참조.
@Slf4j
@RunWith(SpringRunner.class)
@ActiveProfiles(inheritProfiles = false, resolver = ActiveProfileResolver.class)
@SpringBootTest(classes = ApiApplication.class)
public class SpringELTest {
@Value("${app.el.demo.unknown:empty}")
private String empty;
@Value("${app.el.demo.default}")
private String defaultText;
@Value("${app.el.demo.comma}")
private String[] elArray;
@Value("#{'${app.el.demo.comma}'.split(',')}")
private List<String> elList;
@Value("#{systemProperties['java.io.tmpdir']}")
private String spelValue;
@Value("#{${app.el.demo.mapv}}")
private Map<String, String> mapv;
@Value("#{${app.el.demo.mapv}.key1}")
private String key1Value;
@Test
public void test_EL() {
log.info("resource string empty :: {}", empty);
log.info("resource string defaultText :: {}", defaultText);
log.info("resource string elArray :: {}", Lists.newArrayList(elArray));
log.info("resource string elList :: {}", elList);
log.info("resource string spelValue :: {}", spelValue);
log.info("resource string mapv :: {}", mapv);
log.info("resource string key1Value :: {}", key1Value);
}
}
결과
13:13:35.991 [main] INFO c.t.e.mart.utils.test.SpringELTest - resource string empty :: empty
13:13:35.991 [main] INFO c.t.e.mart.utils.test.SpringELTest - resource string defaultText :: default text
13:13:35.991 [main] INFO c.t.e.mart.utils.test.SpringELTest - resource string elArray :: [el1, el2, el3, el4]
13:13:35.991 [main] INFO c.t.e.mart.utils.test.SpringELTest - resource string elList :: [el1, el2, el3, el4]
13:13:35.991 [main] INFO c.t.e.mart.utils.test.SpringELTest - resource string spelValue :: /var/folders/sk/nwgyzphs5hq0g49dnfgpwdsw0000gq/T/
13:13:35.991 [main] INFO c.t.e.mart.utils.test.SpringELTest - resource string mapv :: {key1=1, key2=2}
13:13:35.991 [main] INFO c.t.e.mart.utils.test.SpringELTest - resource string key1Value :: 1