티스토리 뷰
SpEL은 Spring Expression Language의 줄임말로 스프링의 객체들의 정보를 질의하거나 조작하여 어떤 값을 표현할 수 있는 강력한 표현 언어이다. 객체들의 정보는 레퍼런스로 연관되어 있는 객체 그래프를 탐색하여 얻어지므로 런타임 때 SpEL의 표현식 값이 결정(Resolve)된다. 참고로 객체 그래프는 런타임 때 객체 간의 연관 관계를 통해 그려지는 그래프를 의미하는 말이다.
객체 그래프 예시 @Value 어노테이션
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Value("#{1 + 1}")
int value;
@Value("#{'hello' + 'world'}")
String greeting;
@Value("#{1 eq 1}")
boolean trueOrFalse;
@Value("${my.value}")
int myValue;
@Value("#{${my.value} eq 100}")
boolean isMyValue100;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(value);
System.out.println(greeting);
System.out.println(trueOrFalse);
System.out.println(myValue);
System.out.println(isMyValue100);
}
}
2
helloworld
true
100
true
위와 같이 @Value 어노테이션을 사용하여 spEL을 사용할 수도 있다. #{ }을 이용하는건 표현식을 사용하고, ${ }을 이용하는건 프로퍼티를 사용한다. 중간에 보면 "${my.value}가 있는데 이것은 스프링부트 프로젝트에 보면 application.properties라는 파일이 존재한다. 거기에 my.value = 100이라고 작성되어 있는 상태에서 저렇게 값을 가져오는 방법도 존재한다. 그리고 표현식은 안에 프로퍼티를 가질 수 있지만 반대는 안된다는 점 알아두자.
Bean의 정보를 참조하기
import org.springframework.stereotype.Component;
@Component
public class Sample {
private int data = 200;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Value("#{sample.data}")
int sampleData;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(sampleData);
}
}
200
Sample 클래스가 Bean으로 등록되어 있을 때 @Value 어노테이션을 이용해서 위와 같이 사용한다면 Bean에 있는 값을 가져오는 방법도 가능하다.
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("2 + 100");
Integer value = expression.getValue(Integer.class);
System.out.println(value);
}
}
위에 코드는 ExpressionParser를 이용하여 spEL를 사용하는 방법도 가볍게 받아들이면서 알아두자
# 인프런 강의 (백기선 / 스프링 프레임워크 핵심 기술)
Resource 추상화 https://joinwithyou.tistory.com/34
Validation 추상화 https://joinwithyou.tistory.com/35
데이터 바인딩 추상화: PropertyEditor https://joinwithyou.tistory.com/36
데이터 바인딩 추상화: Converter와 Formatter https://joinwithyou.tistory.com/37
SpEL (스프링 Expression Language) https://joinwithyou.tistory.com/38
스프링 AOP: 개념 소개 https://joinwithyou.tistory.com/39
스프링 AOP: 프록시 기반 AOP https://joinwithyou.tistory.com/39
스프링 AOP: @AOP https://joinwithyou.tistory.com/39
Null-safety https://joinwithyou.tistory.com/39
# Reference
https://devlog-wjdrbs96.tistory.com/165?category=882236 (블로그)
https://www.inflearn.com/course/spring-framework_core (백기선 강의)
'Spring' 카테고리의 다른 글
[망나니 개발자] AOP의 이해 (0) | 2021.06.08 |
---|---|
스프링 AOP: 개념 소개 (0) | 2021.06.07 |
데이터 바인딩 추상화: Converter와 Formatter (0) | 2021.06.07 |
데이터 바인딩 추상화: PropertyEditor (0) | 2021.06.07 |
[Spring] Validation 추상화 (0) | 2021.06.07 |