Loading...
Spring Framework Reference Documentation 7.0.2의 Variables의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
expression에서 변수를 참조하려면 #variableName syntax를 사용하면 됩니다. 변수는
EvaluationContext 구현에서 setVariable() 메서드를 사용하여 설정됩니다.
Variable name은 아래에 정의된 문자, underscore, 또는 dollar sign으로 시작해야 합니다.<br>Variable name은 하나 이상의 다음에 나열된 지원되는 유형의 문자로 구성되어야 합니다.<br>- letter:
java.lang.Character.isLetter(char)가true를 반환하는 모든 문자<br> - 여기에는A에서Z,a에서z,ü,ñ,é와 같은 letter는 물론 Chinese, Japanese, Cyrillic 등 다른 문자 집합의 letter도 포함됩니다.<br>- digit:0에서9<br>- underscore:_<br>- dollar sign:$
EvaluationContext에서 variable 또는 root context 객체를 설정할 때, variable 또는 root context 객체의 타입을public으로 하는 것이 좋습니다.<br>그렇지 않으면, non-public 타입을 가진 variable 또는 root context 객체가 포함된 특정 유형의 SpEL expression은 evaluate 또는 compile에 실패할 수 있습니다.
evaluation context에서 variable은 functions와 common namespace를 공유하므로, variable name과 function name이 겹치지 않도록 주의해야 합니다.
다음 예제는 variable을 사용하는 방법을 보여줍니다.
1Inventor tesla = new Inventor("Nikola Tesla", "Serbian"); 2 3EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build(); 4context.setVariable("newName", "Mike Tesla"); 5 6parser.parseExpression("name = #newName").getValue(context, tesla); 7System.out.println(tesla.getName()); // "Mike Tesla"
1val tesla = Inventor("Nikola Tesla", "Serbian") 2 3val context = SimpleEvaluationContext.forReadWriteDataBinding().build() 4context.setVariable("newName", "Mike Tesla") 5 6parser.parseExpression("name = #newName").getValue(context, tesla) 7println(tesla.name) // "Mike Tesla"
#this and #root Variables#this variable은 항상 정의되어 있으며 현재 evaluation 객체를 가리킵니다
(qualifier가 없는 reference가 resolve되는 대상). #root variable은 항상
정의되어 있으며 root context 객체를 가리킵니다.
expression의 구성 요소가
evaluate됨에 따라 #this는 달라질 수 있지만, #root는 항상 root를 가리킵니다.
다음 예제는
collection selection과 함께 #this variable을 사용하는 방법을 보여줍니다.
1// Create a list of prime integers. 2List<Integer> primes = List.of(2, 3, 5, 7, 11, 13, 17); 3 4// Create parser and set variable 'primes' as the list of integers. 5ExpressionParser parser = new SpelExpressionParser(); 6EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build(); 7context.setVariable("primes", primes); 8 9// Select all prime numbers > 10 from the list (using selection ?{...}). 10String expression = "#primes.?[#this > 10]"; 11 12// Evaluates to a list containing [11, 13, 17]. 13List<Integer> primesGreaterThanTen = 14 parser.parseExpression(expression).getValue(context, List.class);
1// Create a list of prime integers. 2val primes = listOf(2, 3, 5, 7, 11, 13, 17) 3 4// Create parser and set variable 'primes' as the list of integers. 5val parser = SpelExpressionParser() 6val context = SimpleEvaluationContext.forReadWriteDataBinding().build() 7context.setVariable("primes", primes) 8 9// Select all prime numbers > 10 from the list (using selection ?{...}). 10val expression = "#primes.?[#this > 10]" 11 12// Evaluates to a list containing [11, 13, 17]. 13val primesGreaterThanTen = parser.parseExpression(expression) 14 .getValue(context) as List<Int>
다음 예제는
collection projection과 함께 #this와 #root variable을 함께 사용하는 방법을 보여줍니다.
1// Create parser and evaluation context. 2ExpressionParser parser = new SpelExpressionParser(); 3EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build(); 4 5// Create an inventor to use as the root context object. 6Inventor tesla = new Inventor("Nikola Tesla"); 7tesla.setInventions("Telephone repeater", "Tesla coil transformer"); 8 9// Iterate over all inventions of the Inventor referenced as the #root 10// object, and generate a list of strings whose contents take the form 11// "<inventor's name> invented the <invention>." (using projection !{...}). 12String expression = "#root.inventions.![#root.name + ' invented the ' + #this + '.']"; 13 14// Evaluates to a list containing: 15// "Nikola Tesla invented the Telephone repeater." 16// "Nikola Tesla invented the Tesla coil transformer." 17List<String> results = parser.parseExpression(expression) 18 .getValue(context, tesla, List.class);
1// Create parser and evaluation context. 2val parser = SpelExpressionParser() 3val context = SimpleEvaluationContext.forReadWriteDataBinding().build() 4 5// Create an inventor to use as the root context object. 6val tesla = Inventor("Nikola Tesla") 7tesla.setInventions("Telephone repeater", "Tesla coil transformer") 8 9// Iterate over all inventions of the Inventor referenced as the #root 10// object, and generate a list of strings whose contents take the form 11// "<inventor's name> invented the <invention>." (using projection !{...}). 12val expression = "#root.inventions.![#root.name + ' invented the ' + #this + '.']" 13 14// Evaluates to a list containing: 15// "Nikola Tesla invented the Telephone repeater." 16// "Nikola Tesla invented the Tesla coil transformer." 17val results = parser.parseExpression(expression) 18 .getValue(context, tesla, List::class.java)
Constructors
Functions