- Spring boot ํ๋ก์ ํธ๊ฐ ์ปค์ง๋ฉด์ ๊ณตํต์ผ๋ก ์ฌ์ฉ๋๋ ๊ธ๋ก๋ฒ ๊ฐ์ ๋ณ๋๋ก ๊ด๋ฆฌํ ํ์๊ฐ ๋ฐ์
@Value ์ด๋
ธํ
์ด์
- properties (๋๋ yaml) ํ์ผ์ ์ํ
ํ ๋ด์ฉ์ ๋ณ์์ ์ฃผ์
ํ๋ ์ญํ ์ํ
- ์ผ๋ฐ์ ์ธ ์
๋ ฅ๊ฐ ์ฃผ์
public class TestClass {
@Value("Hello World!")
private String helloWorld;
@Value("1.234")
private double doubleValue;
@Value("123")
private Integer integerValue;
@Value("true")
private Booleans booleanValue;
@Value("20L")
private long longValue;
}
${...} ์ฌ์ฉ: properties ๊ฐ ์ฃผ์
- ํด๋น ์์ฑ๊ฐ์ ๋ฐํ์์ ๋ณ์๋ก ์ ์ฅ๋๋ฉฐ, ์์ฑ๊ฐ์ด properties ํ์ผ์ ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฐ์
# application.properties
hello.message=Hello World!
public class TestClass {
@Value("${hello.message}")
private String helloWorld;
}
public class TestService {
private String val1;
private String val2;
// ํ๋ผ๋ฏธํฐ๋ก ๋๊ธฐ๋ฉด์ ์ฃผ์
ํ ์ ์๋ค.
public TestService(
@Value("val1") String val1,
@Value("val2") String val2
) {
this.val1 = val1;
this.val2 = val2;
}
}
${...} ์ฌ์ฉํ์ฌ List ์ฃผ์
# application properties
color.list=red,blue,green
public class TestClass {
@Value("${color.list}")
private List<String> colorList;
}
#{${...}} ์ฌ์ฉํ์ฌ Map ์ฃผ์
# application properties
my.config={url:'http://localhost:8081', profile:'local', key:'123456'}
public class TestClass {
@Value("#{${my.config: {url:'http://localhost:8081', profile:'local', key:'123456'}}}")
private Map<String, String> configMap;
}