파란하늘의 지식창고
반응형

Spring은 SpEL (Spring Expression Language)를 제공하고 있다.

Spring Expression Language (SpEL)

RequestMapping이나 value binding 등 Spring을 사용하는 수많은 부분에 SpEL을 사용할 수 있고 다양한 기능을 지원한다.

  • Literal expressions
  • Boolean and relational operators
  • Regular expressions
  • Class expressions
  • Accessing properties, arrays, lists, and maps
  • Method invocation
  • Assignment
  • Calling constructors
  • Bean references
  • Array construction
  • Inline lists
  • Inline maps
  • Ternary operator
  • Variables
  • User-defined functions added to the context
  • reflective invocation of Method
  • various cases of MethodHandle
  • Collection projection
  • Collection selection
  • Templated expressions

Spring이 제공하는 SpEL 문법을 개별적으로 사용하고 싶은 경우 Spring Framework가 3.0.0부터 제공하는 SpelExpressionParser를 다음과 같이 사용하면 된다.

var expressionParser = new SpelExpressionParser();
var evaluationContext = new StandardEvaluationContext();

evaluationContext.setVariable("testKey", "testValue");
Expression expression = expressionParser.parseExpression("'Any string : ' + #testKey");

String result = (String) expression.getValue(evaluationContext);
// result : Any string : testValue

evaluationContext를 생성하여 임의의 값을 추가하고 SpEL로 해당 값을 사용하는 경우의 예제이다.

반응형
profile

파란하늘의 지식창고

@Bluesky_

내용이 유익했다면 광고 배너를 클릭 해주세요