스프링 표현 언어(SpEL)는 런타임에 객체 그래프를 탐색하고 조작할 수 있도록 해주는 강력한 표현 언어이다.
자바 표현 언어는 여러가지(OGNL, MVEL, JBoss EL 등)가 있지만, SpEL은 스프링 프로덕트 전반적으로 사용할 수 있는 좋은 단일 표현 언어로 스프링 커뮤니티에 제공되었다.
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue(); // "Hello World"
SpelParser는 "" 안에 들어있는 문자열을 평가(evaluation)해서 결과값을 만들어낸다.
Expression expWow = parser.parseExpression("'Hello World'.concat('!')");
String messageWow = (String) expWow.getValue(); // "Hello World!"
SpEL은 메서드를 호출하거나 속성에 접근하거나 생성자를 호출하는 등의 다양한 기능을 지원한다. concat 메서드를 호출하는 예제이다.
Expression expString =
parser.parseExpression("new String('hello world').toUpperCase()");
String messageString = expString.getValue(String.class); // "HELLO WORLD"
생성자를 호출할 수도 있다.
#{<expression string> }
방식으로 property를 설정${ <property name> }
방식으로 가져옴@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
}
출처: https://wbluke.tistory.com/67 [함께 자라기]