当前位置:   article > 正文

Spring源码:Spring EL表达式源码分析_beanexpressionresolver

beanexpressionresolver

目录

1.EL表达式处理过程

1.1 根据EL表达式字符串构建抽象语法树(ast)

1.2 构造标准评估上下文对象StandardEvaluationContext

1.3 利用标准评估上下文对象StandardEvaluationContext解析EL表达式语法树

2.Spring框架中如何使用到EL表达式

3.应用EL表达式的条件注解@OnExpressionCondition


1.EL表达式处理过程

首先看一下EL表达式在代码中的使用方式:

  1. BeanExpressionContext expressionContext = new BeanExpressionContext(beanFactory, null);
  2. Object result = new StandardBeanExpressionResolver().evaluate(expression, expressionContext);

BeanExpressionContext:表示bean表达式上下文对象,用于获取bean对象,这里表示从beanFactory工厂中获取指定bean名称的bean对象;

StandardBeanExpressionResolver:表示标准的bean表达式解析器,实现接口BeanExpressionResolver;对EL表达式字符串,在指定的bean表达式上下文中进行解析;

下面主要看一下BeanExpressionResolver的接口方法evaluate在StandardBeanExpressionResolver的实现逻辑:

  1. /**
  2. * Evaluate the given value as an expression, if applicable;
  3. * return the value as-is otherwise.
  4. * @param value the value to check
  5. * @param evalContext the evaluation context
  6. * @return the resolved value (potentially the given value as-is)
  7. * @throws BeansException if evaluation failed
  8. */
  9. @Override
  10. @Nullable
  11. public Object evaluate(@Nullable String value, BeanExpressionContext evalContext) throws BeansException {
  12. if (!StringUtils.hasLength(value)) {
  13. return value;
  14. }
  15. try {
  16. Expression expr = this.expressionCache.get(value);
  17. if (expr == null) {
  18. expr = this.expressionParser.parseExpression(value, this.beanExpressionParserContext);
  19. this.expressionCache.put(value, expr);
  20. }
  21. StandardEvaluationContext sec = this.evaluationCache.get(evalContext);
  22. if (sec == null) {
  23. sec = new StandardEvaluationContext(evalContext);
  24. sec.addPropertyAccessor(new BeanExpressionContextAccessor());
  25. sec.addPropertyAccessor(new BeanFactoryAccessor());
  26. sec.addPropertyAccessor(new MapAccessor());
  27. sec.addPropertyAccessor(new EnvironmentAccessor());
  28. sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
  29. sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
  30. ConversionService conversionService = evalContext.getBeanFactory().getConversionService();
  31. if (conversionService != null) {
  32. sec.setTypeConverter(new StandardTypeConverter(conversionService));
  33. }
  34. customizeEvaluationContext(sec);
  35. this.evaluationCache.put(evalContext, sec);
  36. }
  37. return expr.getValue(sec);
  38. }
  39. catch (Throwable ex) {
  40. throw new BeanExpressionException("Expression parsing failed", ex);
  41. }
  42. }

这里主要包含以下几步:

1.1 根据EL表达式字符串构建抽象语法树(ast)

1.2 构造标准评估上下文对象StandardEvaluationContext

1.3 利用标准评估上下文对象StandardEvaluationContext解析EL表达式语法树,得出计算结果

下面分别进行说明:

1.1 根据EL表达式字符串构建抽象语法树(ast)

构建抽象语法树的关键代码如下:InternalSpelExpressionParser

  1. @Override
  2. protected SpelExpression doParseExpression(String expressionString, ParserContext context) throws ParseException {
  3. try {
  4. this.expressionString = expressionString;
  5. Tokenizer tokenizer = new Tokenizer(expressionString);
  6. tokenizer.process();
  7. this.tokenStream = tokenizer.getTokens();
  8. this.tokenStreamLength = this.tokenStream.size();
  9. this.tokenStreamPointer = 0;
  10. this.constructedNodes.clear();
  11. SpelNodeImpl ast = eatExpression();
  12. if (moreTokens()) {
  13. throw new SpelParseException(peekToken().startPos, SpelMessage.MORE_INPUT, toString(nextToken()));
  14. }
  15. Assert.isTrue(this.constructedNodes.isEmpty());
  16. return new SpelExpression(expressionString, ast, this.configuration);
  17. }
  18. catch (InternalParseException ex) {
  19. throw ex.getCause();
  20. }
  21. }

这里也主要分为2步:

1.根据字符串表达式识别出token流,简单来说就是一个分词的过程;

2.根据构建的token流构建抽象语法树,这里按照EL表达式支持的运算符的优先级进行解析,构建了一个树形结构,每个节点都表示一个运算单元(操作符和若干操作数),比如OpPlus(加法运算),OpMinus(减法运算);

最终的构造的语法数的根节点表示整个表达式的优先级最低的运算单元,比如:1+(2-3),根节点即是:OpPlus,加法运算,两个操作数为1 和(2-3);

1.2 构造标准评估上下文对象StandardEvaluationContext

StandardEvaluationContext对象主要包含了一下几部分功能:

  • beanFactory(用于获取指定bean)

  • environment用于获取指定属性

  • map用于获取map结构的值

  • ...

  • 并且提供了可扩展的接口方法customizeEvaluationContext

1.3 利用标准评估上下文对象StandardEvaluationContext解析EL表达式语法树

这里利用标准评估上下文对象StandardEvaluationContext来对抽象语法树进行解析,实际是一个深度优先搜索的计算过程,最终返回整个表达式的计算结果;

  1. @Override
  2. @Nullable
  3. public Object getValue(EvaluationContext context) throws EvaluationException {
  4. Assert.notNull(context, "EvaluationContext is required");
  5. ExpressionState expressionState = new ExpressionState(context, this.configuration);
  6. Object result = this.ast.getValue(expressionState);
  7. checkCompile(expressionState);
  8. return result;
  9. }

2.Spring框架中如何使用到EL表达式

Spring在启动时,会配置好SpEl表达式引擎,具体过程是在AbstractApplicationContext的refresh方法当中会对beanFactory进行配置,如下:

  1. /**
  2. * Configure the factory's standard context characteristics,
  3. * such as the context's ClassLoader and post-processors.
  4. * @param beanFactory the BeanFactory to configure
  5. */
  6. protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  7. // Tell the internal bean factory to use the context's class loader etc.
  8. beanFactory.setBeanClassLoader(getClassLoader());
  9. beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
  10. beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
  11. // Configure the bean factory with context callbacks.
  12. beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
  13. beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
  14. beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
  15. beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
  16. beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
  17. beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
  18. // BeanFactory interface not registered as resolvable type in a plain factory.
  19. // MessageSource registered (and found for autowiring) as a bean.
  20. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
  21. beanFactory.registerResolvableDependency(ResourceLoader.class, this);
  22. beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
  23. beanFactory.registerResolvableDependency(ApplicationContext.class, this);
  24. // Detect a LoadTimeWeaver and prepare for weaving, if found.
  25. if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
  26. beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
  27. // Set a temporary ClassLoader for type matching.
  28. beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
  29. }
  30. // Register default environment beans.
  31. if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
  32. beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
  33. }
  34. if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
  35. beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
  36. }
  37. if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
  38. beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
  39. }
  40. }

3.应用EL表达式的条件注解@OnExpressionCondition

@OnExpressionCondition 是Spring提供的根据表达式计算结果来有条件加载bean的一种方式;

  1. /**
  2. * Configuration annotation for a conditional element that depends on the value of a SpEL
  3. * expression.
  4. *
  5. * @author Dave Syer
  6. * @since 1.0.0
  7. */
  8. @Retention(RetentionPolicy.RUNTIME)
  9. @Target({ ElementType.TYPE, ElementType.METHOD })
  10. @Documented
  11. @Conditional(OnExpressionCondition.class)
  12. public @interface ConditionalOnExpression {
  13. /**
  14. * The SpEL expression to evaluate. Expression should return {@code true} if the
  15. * condition passes or {@code false} if it fails.
  16. * @return the SpEL expression
  17. */
  18. String value() default "true";
  19. }

主要实现代码如下,包括占位符解析,SpEL表达式计算;

  1. /**
  2. * A Condition that evaluates a SpEL expression.
  3. *
  4. * @author Dave Syer
  5. * @author Stephane Nicoll
  6. * @see ConditionalOnExpression
  7. */
  8. @Order(Ordered.LOWEST_PRECEDENCE - 20)
  9. class OnExpressionCondition extends SpringBootCondition {
  10. @Override
  11. public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
  12. String expression = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName())
  13. .get("value");
  14. expression = wrapIfNecessary(expression);
  15. ConditionMessage.Builder messageBuilder = ConditionMessage.forCondition(ConditionalOnExpression.class,
  16. "(" + expression + ")");
  17. expression = context.getEnvironment().resolvePlaceholders(expression);
  18. ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
  19. if (beanFactory != null) {
  20. boolean result = evaluateExpression(beanFactory, expression);
  21. return new ConditionOutcome(result, messageBuilder.resultedIn(result));
  22. }
  23. return ConditionOutcome.noMatch(messageBuilder.because("no BeanFactory available."));
  24. }
  25. private Boolean evaluateExpression(ConfigurableListableBeanFactory beanFactory, String expression) {
  26. BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();
  27. if (resolver == null) {
  28. resolver = new StandardBeanExpressionResolver();
  29. }
  30. BeanExpressionContext expressionContext = new BeanExpressionContext(beanFactory, null);
  31. Object result = resolver.evaluate(expression, expressionContext);
  32. return (result != null && (boolean) result);
  33. }
  34. /**
  35. * Allow user to provide bare expression with no '#{}' wrapper.
  36. * @param expression source expression
  37. * @return wrapped expression
  38. */
  39. private String wrapIfNecessary(String expression) {
  40. if (!expression.startsWith("#{")) {
  41. return "#{" + expression + "}";
  42. }
  43. return expression;
  44. }
  45. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/534012
推荐阅读
相关标签
  

闽ICP备14008679号