SpEL
- ๊ฐ์ฒด๋ฅผ ์กฐํํ๊ณ ์กฐ์ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณต
- ๋ฉ์๋ ํธ์ถ, ๋ฌธ์์ด ํ
ํ๋ฆฟ ๊ธฐ๋ฅ ๋ฑ์ ์ฌ๋ฌ๊ฐ์ง ์ถ๊ฐ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํํ์ ์ธ์ด
- Spring ํ๋ก์ ํธ ์ ๋ฐ์ ๊ฑธ์ณ ์ฌ์ฉ(3.0+)
#{ SpELํํ์ }
- ์คํ๋ง์์
${ }
ํ๊ธฐ๋ ์ฌ์ฉ๋๋๋ฐ ์ด๋ SpEL์ด ์๋๋ผ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ ๋ ์ฌ์ฉ
์ง์ ๊ธฐ๋ฅ
- ๋ฆฌํฐ๋ด ํํ์ (Literal Expression)
- Boolean๊ณผ ๊ด๊ณ์ฐ์ฐ์ (Boolean and Relational Operator)
- ์ ๊ท ํํ์ (Regular Expression)
- ํด๋์ค ํํ์ (Class Expression)
- ํ๋กํผํฐ, ๋ฐฐ์ด, ๋ฆฌ์คํธ, ๋งต์ ๋ํ ์ ๊ทผ ์ง์ (Accessing properties, arrays, lists, maps)
- ๋ฉ์๋ ํธ์ถ (Method Invocation)
- ๊ด๊ณ์ฐ์ฐ์ (Relational Operator)
- ํ ๋น (Assignment)
- ์์ฑ์ ํธ์ถ (Calling Constructors)
- Bean ์ฐธ์กฐ (Bean References)
- ๋ฐฐ์ด ์์ฑ (Array Contruction)
- ์ธ๋ผ์ธ ๋ฆฌ์คํธ/๋งต (Inline List/Map)
- ์ผํญ ์ฐ์ฐ์ (Ternary Operator)
- ๋ณ์ (Variables)
- ์ฌ์ฉ์ ์ ์ ํจ์ (User defined functions)
- Collections Projection
- Collections Selection
- Templated expression
์ํ
@Value("#{1+1}")
int value;
@Value("#{'hello ' + 'world'}")
String greeting;
@Value("#{1 eq 5}")
boolean trueOrFalse;
@Value("Literal String")
String literalString;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(value);
System.out.println(greeting);
System.out.println(trueOrFalse);
System.out.println(literalString);
}
Properties
my.value=100
@Value("#{'${my.value}' eq '100'}")
boolean isEqual;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(isEqual);
}
ExpressionParser
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("1+1");
Object value = expression.getValue();
System.out.println(value); // 2
Language Reference
- Literal Expressions
- Properties, Arrays, Lists, Maps, and Indexers
- Inline Lists
- Inline Maps
- Array Construction
- Methods
- Operators
- Types
- Constructors
- Variables
- Functions
- Bean References
- Ternary Operator (If-Then-Else)
- The Elvis Operator
- Safe Navigation Operator
์ํ
ExpressionParser parser = new SpelExpressionParser();
// evals to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
// evals to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
Object nullValue = parser.parseExpression("null").getValue();
ref