SpEL, Spring Expression Language
Expression Language ๋ ์งง๊ณ ๊ฐ๋จํ ๋ฌธ๋ฒ์ ํตํด ํ์ํ ๋ฐ์ดํฐ๋ ์ค์ ๊ฐ์ ์ป์ด์ฌ ์ ์๊ฒ ํ๋ ํน๋ณํ ํํ์ ํํ์์ด๋ค. SpEL์ ๊ทธ ์ค์์๋ ์คํ๋ง ๋ชจ๋ ์์ญ์์ ์ฌ์ฉ ๊ฐ๋ฅํ ์ธ์ด ํ์
์ฃผ๋ก @Value("${config.value}") ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ค์ ๊ฐ์ ์ฃผ์ ๋ฐ๋๋ฐ ํ์ฉ
SpEL์ ๊ฐ ํ๊ฐ (evaluation)
SpelParser๋ "" ์์ ๋ค์ด์๋ ๋ฌธ์์ด์ ํ๊ฐํด์ (evaluation) ๊ฒฐ๊ณผ๊ฐ์ ๋ง๋ค์ด๋ธ๋ค.
'Hello World' ๋ ๋ฌธ์์ด ๋ฆฌํฐ๋ด์ด ๋๋ฉฐ, concat ์ด๋ผ๋ ๋ฉ์๋๋ ํธ์ถํ ์ ์๋ค.
String ๊ฐ์ฒด๋ฅผ new ๋ก ์์ฑํด์ ์ฌ์ฉ๋ ๊ฐ๋ฅ
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue(); // "Hello World"
Expression expWow = parser.parseExpression("'Hello World'.concat('!')");
String messageWow = (String) expWow.getValue(); // "Hello World!"
Expression expString =
parser.parseExpression("new String('hello world').toUpperCase()");
String messageString = expString.getValue(String.class); // "HELLO WORLD"
Bean์ Property๋ฅผ ์ค์ ํ ๋ ์ฌ์ฉํ๋ ๋ฐฉ์
๊ธฐ๋ณธ์ ์ผ๋ก #{ } ๋ฐฉ์์ผ๋ก property๋ฅผ ์ค์
application.properties (๋๋ application.yml) ์ ๊ฐ์ ๊ฐ์ ธ์ฌ ๋๋ ${ } ๋ฐฉ์์ผ๋ก ๊ฐ์ ธ์ด
@Component
public class SimpleComponent {
@Value("#{ 1+1 }")
int two; // 2
@Value("#{ 2 eq 2 }")
boolean isTrue; // true
@Value("${ server.hostname }")
String hostName; // www.server.com
@Value("#{ ${ server.hostname } eq 'www.server.com'}")
boolean isHostSame; // true
}
Last updated
Was this helpful?