์ด ๋ฌธ์๋ ๊ฐ์ธ์ ์ธ ๋ชฉ์ ์ด๋ ๋ฐฐํฌํ๊ธฐ ์ํด์ ๋ณต์ฌํ ์ ์๋ค. ์ถ๋ ฅ๋ฌผ์ด๋ ๋์งํธ ๋ฌธ์๋ ๊ฐ ๋ณต์ฌ๋ณธ์ ์ด๋ค ๋น์ฉ๋ ์ฒญ๊ตฌํ ์ ์๊ณ ๋ชจ๋ ๋ณต์ฌ๋ณธ์๋ ์ด ์นดํผ๋ผ์ดํธ ๋ฌธ๊ตฌ๊ฐ ์์ด์ผ ํ๋ค. (์ถ์ฒ)
SpEL - Spring Expression Language
๊ฐ์ฒด๋ฅผ ์กฐํํ๊ณ ์กฐ์ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฉฐ, ๋ฉ์๋ ํธ์ถ, ๋ฌผ์์ด ํ ํ๋ฆฟ ๊ธฐ๋ฅ ๋ฑ์ ์ฌ๋ฌ๊ฐ์ง ์ถ๊ฐ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํํ์ ์ธ์ด์ด๋ค. OGNL ๋ฑ ์๋ฐ์์ ์ฌ์ฉ๊ฐ๋ฅํ ์ฌ๋ฌ EL์ด ์์ง๋ง, SpEL์ Spring ํ๋ก์ ํธ ์ ๋ฐ์ ๊ฑธ์ณ ์ฌ์ฉํ๊ธฐ ์ํด ๋ง๋ค์ด์ก์ผ๋ฉฐ ์คํ๋ง 3.0๋ถํฐ ์ง์ํ๋ค.
SpEL ํ๊ธฐ๋ฒ
#{SpEL ํํ์}#{}์์ ํํ์์ evaluation ํ๋ค.- ์คํ๋ง์์ ์ฌ์ฉ๋๋
${}์ SpEL์ด ์๋๋ผ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ ๋ ์ฌ์ฉํ๋ ํ๊ธฐ์ด๋ค. SpEL์ ๊ธฐ๋ณธ์ ์ผ๋ก#{}์ผ๋ก ํ๊ธฐํ๋ค
- ์คํ๋ง์์ ์ฌ์ฉ๋๋
SpEL ํ์ฑ
Expression์ ์ด์ฉํ SpEL ํ์ฑ
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("1+1");
Object value1 = expression.getValue();
System.out.println(value1); // 2
int value2 = expression.getValue(Integer.class);
System.out.println(value2); // 2- ExpressionParser์ ๊ตฌํ์ฒด์ธ SpelExpressionParser๋ก SpEL์ ๋ด์ฉ์ ํ์ฑํ๊ณ , Expression์ getValue() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํ์ฑ๋ ๊ฒฐ๊ณผ๋ฌผ์ Object ํ์ ์ผ๋ก ์ป์ ์ ์๋ค.
- getValue() ๋ฉ์๋์ ํด๋์ค๋ฅผ ๋ฃ์ผ๋ฉด ํ์ ์บ์คํ ๋ ๊ฐ๋ฅํ๋ค.
EvaluationContext๋ฅผ ์ด์ฉํ SpEL ํ์ฑ
๋จ์ ํํ์ ํ์ฑ์ด ์๋ ๊ฐ์ฒด ์ ๋ณด๋ค์ context๊ฐ ํ์ํ ๊ฒฝ์ฐ EvaluationContext๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
// name, nationality๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๊ฐ๋ ์์ฑ์
Inventor tesla = new Inventor("Nikola Tesla","Serbian");
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("name"); // name ํ๋กํผํฐ
// Context์ tesla๊ฐ์ฒด๋ฅผ ๋ฃ์ด์ค๋ค.
EvaluationContext context = new StandardEvaluationContext(tesla);
String name1 = (String) exp.getValue(context); //name = "Nikola Tesla"
System.out.println(name1); // Nikola Tesla
// getValue ๋ฉ์๋ ํธ์ถ ์ StandardEvaluationContext๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ฐ์ฒด๋ฅผ ์ง์ ์ง์
String name2 = (String) exp.getValue(tesla);
System.out.println(name2); // Nikola Tesla- StandardEvaluationContext์ name ํ๋กํผํฐ๊ฐ ํ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ์ง์ ํ๋ค.
- ๋๋ฒ์งธ ๋ฐฉ๋ฒ์ฒ๋ผ StandardEvaluationContext๋ฅผ ์ฌ์ฉํ์ง ์๊ณ getValue()์ ์ง์ ๊ฐ์ฒด๋ฅผ ์ง์ ํ ์ ์๋ค. ํ์ง๋ง StandardEvaluationContext๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด ์์ฑ ๋น์ฉ์ ๋ค์ง๋ง, ํ๋์ ๋ํด ์บ์ฑํ๊ธฐ ๋๋ฌธ์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉํ๋ฉด ํํ์ ํ์ฑ์ด ๋ ๋น ๋ฅด๋ค๋ ์ฅ์ ์ด ์๋ค.
SpEL ๊ธฐ๋ฅ ๊ฐ์
๋ฆฌํฐ๋ด ํํ์
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();ํ๋กํผํฐ, ๋ฐฐ์ด, ๋ฆฌ์คํธ, ๋งต์ ๋ํ ์ ๊ทผ
int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
String name = parser.parseExpression("Members[0].Name")
.getValue(societyContext, String.class);
// Officer์ ๋์
์ด๋ฆฌ(๋งต ์ ๊ทผ)
Inventor pupin = parser.parseExpression("Officers['president']")
.getValue(societyContext, Inventor.class);
// ๊ฐ์ ์ค์ ํ๋ค
parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country")
.setValue(societyContext, "Croatia");
// ์์ ํ ํ์ ์ฐ์ฐ์
city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);- javascript์
optional chaining์ฒ๋ผ null์ด ์๋์ง ํ์ธํ๋ฉฐ ํ๋กํผํฐ์ ์ ๊ทผํ๋ ๋ฌธ๋ฒ๋ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
๋ฉ์๋ ํธ์ถ
String c = parser.parseExpression("'abc'.substring(2, 3)").getValue(String.class);
boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')")
.getValue(societyContext, Boolean.class);- ๋ฉ์๋ ํธ์ถ์ ์ง์ํ๋ฉฐ, ๊ฐ๋ณ์ธ์๋ ์ง์ํ๋ค.
๊ด๊ณ ์ฐ์ฐ์
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
boolean falseValue = parser.parseExpression("'xyz' instanceof T(int)")
.getValue(Boolean.class);
// ์ ๊ทํํ์๊ณผ ๋น๊ตํ๊ธฐ ์ํด์ mathes ํค์๋๋ฅผ ์ฌ์ฉํ๋ค.
boolean trueValue = parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'")
.getValue(Boolean.class);๋ ผ๋ฆฌ ์ฐ์ฐ์
boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);์์ ์ฐ์ฐ์
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21ํ ๋น
Inventor inventor = new Inventor();
StandardEvaluationContext inventorContext = new StandardEvaluationContext(inventor);
parser.parseExpression("Name").setValue(inventorContext, "Alexander Seovic2");
// ๋์ ์ getValue()๋ก๋ ํ ๋น์ด ๊ฐ๋ฅํ๋ค.
String aleks = parser.parseExpression("Name = 'Alexandar Seovic'")
.getValue(inventorContext, String.class);์์ฑ์ ํธ์ถ
Inventor einstein = p.parseExpression(
"new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')")
.getValue(Inventor.class);
//๋ฆฌ์คํธ์ add ๋ฉ์๋๋ด์์ ์๋ก์ด inventor ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค
p.parseExpression(
"Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German'))")
.getValue(societyContext);- ์์ฑ์๋ฅผ ์๋ก ๋ง๋ค ์๋ ์๋ค. primitive ํ์ ๊ณผ String ์ธ์๋ ๋ชจ๋ ์ ๊ทํ๋ ํด๋์ค๋ช ์ ์ฌ์ฉํด์ผํ๋ค.
ํ์
Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class);
Class stringClass = parser.parseExpression("T(String)").getValue(Class.class);
boolean
trueValue = parser.parseExpression(
"T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR")
.getValue(Boolean.class);T์ฐ์ฐ์๋ฅผ ํด๋์ค ์ธ์คํด์ค๋ฅผ ์ง์ ํ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค. ์ ์ ๋ฉ์๋๋ ์ด ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ ํธ์ถํ ์ ์๋ค.- StandardEvaluationContext๋ ํ์ ์ ์ฐพ์ผ๋ ค๊ณ TypeLocator๋ฅผ ์ฌ์ฉํ๊ณ StandardTypeLocator(๊ต์ฒดํ ์ ์๋ค)๋ java.lang ํจํค์ง๋ก ๋ง๋ค์ด์ง๋ค. ์ฆ, java.lang ๋ด์์ ํ์ ์ ์ฐธ์กฐํ๋ T()๋ ์ ๊ทํ๋ ํ์๋ ์์ง๋ง ๋ค๋ฅธ ๋ชจ๋ ํ์ ์ฐธ์กฐ๋ ์ ๊ทํ๋์ด์ผ ํ๋ค.
๋น(Bean) ์ฐธ์กฐ
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());
// ํ๊ฐํ๋ ๋์ MyBeanResolver์์ resolve(context,"foo")๋ฅผ ํธ์ถํ ๊ฒ์ด๋ค
Object bean = parser.parseExpression("@foo").getValue(context);- context๊ฐ ๋น ๋ฆฌ์กธ๋ฒ๋ก ์ค์ ๋์ด์๋ค๋ฉด @ ๊ธฐํธ๋ฅผ ์ฌ์ฉํด์ ํํ์์์ ๋น์ ๊ฒ์ํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
๋ฐฐ์ด ์์ฑ
int[] numbers1 = (int[]) parser.parseExpression("new int[4]").getValue(context);
int[] numbers2 = (int[]) parser.parseExpression("new int[]{1,2,3}").getValue(context);
int[][] numbers3 = (int[][]) parser.parseExpression("new int[4][5]").getValue(context);์ธ๋ผ์ธ ๋ฆฌ์คํธ
// 4๊ฐ์ ์ซ์๋ฅผ ๋ด๊ณ ์๋ ์๋ฐ ๋ฆฌ์คํธ๋ก ํ๊ฐ๋๋ค
List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context);์ผํญ ์ฐ์ฐ์
String falseString = parser.parseExpression("false ? 'trueExp' : 'falseExp'")
.getValue(String.class);
// ์๋น์ค ๋ฌธ๋ฒ a ? a : b --> a ?: b
String name = parser.parseExpression("null?:'Unknown'").getValue(String.class);- ์ผํญ์ฐ์ฐ์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๊ณ , ๋จ์ถํ์ธ ์๋น์ค ๋ฌธ๋ฒ๋ ์ฌ์ฉํ ์ ์๋ค.
๋ณ์
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
context.setVariable("newName", "Mike Tesla");
parser.parseExpression("Name = #newName").getValue(context);
System.out.println(tesla.getName()) // "Mike Tesla"- context์ ๋ณ์๋ฅผ ์ ์ธํ์ฌ ํํ์ ๋ด์์ ๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
- ๋ค๋ง
#this์#root๋ณ์๋ ํญ์ ์ ์๋์ด์๋ค. ๊ฐ๊ฐ ํ์ฌ ํ๊ฐ๊ฐ์ฒด, ๋ฃจํธ ์ปจํ ์คํธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ค.
์ฌ์ฉ์ ์ ์ ํจ์
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.registerFunction("reverseString",
StringUtils.class.getDeclaredMethod("reverseString",
new Class[] { String.class }));
String helloWorldReversed =
parser.parseExpression("#reverseString('hello')").getValue(context, String.class);- context์ ํจ์๋ฅผ ๋ฑ๋กํด์ SpEL์ ํ์ฅํ ์ ์๋ค.
์ปฌ๋ ์ ํฌ์(Collection projection)
List placesOfBirth = (List)parser.parseExpression("Members.![placeOfBirth.city]");- ํ์ ํํ์์ ํ๊ฐํด์ ์๋ก์ด ์ปฌ๋ ์ ์ ๋ฐํํ๋ค. ์ปฌ๋ ์ ์ ํน์ ํ๋๋ง์ผ๋ก ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
- ๋งต์์๋ ํฌ์๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๊ณ ๋งต์ ๊ฒฝ์ฐ ํฌ์ ํํ์์ ๋งต์ ๊ฐ ์ํธ๋ฆฌ๋ง๋ค(์๋ฐ Map.Entry๋ก ํํ๋๋) ํ๊ฐ๋๋ค. ๋งต์ ํฌ์๊ฒฐ๊ณผ๋ ๋งต์ ๊ฐ ์ํธ๋ฆฌ์ ๋ํ ํฌ์ ํํ์์ ํ๊ฐ๊ฒฐ๊ณผ๋ก ์ด๋ฃจ์ด์ง ๋ฆฌ์คํธ์ด๋ค
์ปฌ๋ ์ ์ ํ
List<Inventor> list = (List<Inventor>)parser.parseExpression(
"Members.?[Nationality == 'Serbian']")
.getValue(societyContext);
Map newMap = parser.parseExpression("map.?[value<27]").getValue();- ์ปฌ๋ ์ ์ ํํฐ๋งํด์ ์๋ ์์์ ์๋ธ์ ์ ๊ฐ์ง ์๋ก์ด ์ปฌ๋ ์ ์ ๋ฐํํ๋ค.
ํํ์ ํ ํ๋ฆฟ
String randomPhrase =
parser.parseExpression("random number is #{T(java.lang.Math).random()}",
new TemplateParserContext()).getValue(String.class);#{}๋ก ๊ตฌ๋ถํ์ฌ ํ๋ ์ด์์ ํ๊ฐ ๋ธ๋ญ์ ๊ฐ์ง ๋ฆฌํฐ๋ด ๋ฌธ์๋ฅผ ์์ ์ ์๋ค.
์ฌ์ฉ์์
@Value ์ ๋ ธํ ์ด์ ์์ SpEL ์ฌ์ฉ
@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); // 2
System.out.println(greeting); // hello world
System.out.println(trueOrFalse); // false
System.out.println(literalString); // Literal String
}- ๋น์ด ๋ง๋ค์ด์ง ๋, @Value() ์์ ๊ฐ์ด #{} ํ๊ธฐ๋ก ๊ฐ์ธ์ ธ ์์ผ๋ฉด, SpEL๋ก ํ์ฑํ๊ณ ํ๊ฐํด์ ๊ฒฐ๊ณผ๊ฐ์ ๋ณ์์ ํ ๋นํจ
SpEL๊ณผ ํ๋กํผํฐ
my.value=100@Value("#{'${my.value}' eq '100'}")
boolean isEqual;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(isEqual); // true
}๋น ์ฐธ์กฐ
@Component
public class Sample {
private int value = 123;
public int getValue() {
return value;
}
}@Value("#{sample.Value}")
int sampleValue;์ฐธ๊ณ
SpEL๋ ํด๋นํ๋ ํ์ ์ผ๋ก ๋ณํํ ๋ ConversionService๋ฅผ ์ฌ์ฉํ๋ค