Spring系列之Spring表达式语言(SpEL)

Spring,Spring Expression Language,SpEL,类似于Struts2x中使用的OGNL表达式语言(以及jsp的EL)),能在运行时构建复杂表达式、存取对象图属性、对象方法调用等。表达式语言给静态Java语言增加动态功能。SpEL是单独模块,只依赖于core模块,可以单独使用,依赖如下,但是一般无需独立引入,作为spring核心组件,常被其他组件作为基础依赖,实现若干功能:如配合配置Bean定义。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

SpEL支持如下表达式:

  1. 基本表达式:字面量表达式、关系,逻辑与算数运算表达式、字符串连接及截取表达式、三目运算及Elivis表达式、正则表达式、括号优先级表达式;
  2. 类相关表达式:类类型表达式、类实例化、instanceof表达式、变量定义及引用、赋值表达式、自定义函数、对象属性存取及安全导航表达式、对象方法调用、Bean引用;
  3. 集合相关表达式:内联List、内联数组、集合,字典访问、列表,字典,数组修改、集合投影、集合选择;不支持多维内联数组初始化;不支持内联字典定义;
  4. 其他表达式:模板表达式。

注:SpEL表达式中的关键字是不区分大小写的。

通过实例分析SpEL求表达式值的步骤:

public class SpELTest {
    @Test
    public void helloWorld() {
    	// 1.创建解析器, 提供SpelExpressionParser默认实现
        ExpressionParser parser = new SpelExpressionParser();
        // 2.解析表达式, parseExpression方法解析得到Expression对象
        Expression expression = parser.parseExpression("('Hello' + ' World').concat(#end)");
        // 3.构造上下文, 设置数据
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("end", "!");
        // 4.Expression接口的getValue方法
        Assert.assertEquals("Hello World!", expression.getValue(context));
    }
}

SpEL原理及接口

  1. 表达式:表达式是表达式语言的核心,所以表达式语言都是围绕表达式进行的,“干什么”;
  2. 解析器:用于将字符串表达式解析为表达式对象,“谁来干”;
  3. 上下文:表达式对象执行的环境,该环境可能定义变量、定义自定义函数、提供类型转换等等,“在哪干”;
  4. 根对象及活动上下文对象:根对象是默认的活动上下文对象,活动上下文对象表示了当前表达式操作的对象,“对谁干”。

在这里插入图片描述
核心流程:
SpelExpressionParser解析器内部使用Tokenizer类进行词法分析,即把字符串流分析为记号流,记号在SpEL使用Token类来表示;
得到记号流后,解析器便可根据记号流生成内部抽象语法树;
在SpEL中语法树节点由SpelNode接口实现代表:如OpPlus表示加操作节点、IntLiteral表示int型字面量节点;
使用SpelNodel实现组成抽象语法树;
对外提供Expression接口来简化表示抽象语法树,从而隐藏内部实现细节,并提供getValue简单方法用于获取表达式值;
SpEL提供默认实现为SpelExpression;
定义表达式上下文对象(可选),SpEL使用EvaluationContext接口表示上下文对象,用于设置根对象、自定义变量、自定义函数、类型转换器等,SpEL提供默认实现StandardEvaluationContext;

public interface ExpressionParser {
       Expression parseExpression(String expressionString);
       Expression parseExpression(String expressionString, ParserContext context);
}

EvaluationContext接口:

public interface EvaluationContext {
	/**
	 * Return the default root context object against which unqualified
	 * properties/methods/etc should be resolved. This can be overridden
	 * when evaluating an expression.
	 */
	TypedValue getRootObject();

	/**
	 * Return a list of accessors that will be asked in turn to read/write a property.
	 */
	List<PropertyAccessor> getPropertyAccessors();

	/**
	 * Return a list of resolvers that will be asked in turn to locate a constructor.
	 */
	List<ConstructorResolver> getConstructorResolvers();

	List<MethodResolver> getMethodResolvers();

	@Nullable
	BeanResolver getBeanResolver();

	/**
	 * Return a type locator that can be used to find types, either by short or
	 * fully qualified name.
	 */
	TypeLocator getTypeLocator();

	/**
	 * Return a type converter that can convert (or coerce) a value from one type to another.
	 */
	TypeConverter getTypeConverter();

	/**
	 * Return a type comparator for comparing pairs of objects for equality.
	 */
	TypeComparator getTypeComparator();

	/**
	 * Return an operator overloader that may support mathematical operations
	 * between more than the standard set of types.
	 */
	OperatorOverloader getOperatorOverloader();

	void setVariable(String name, @Nullable Object value);

	/**
	 * Look up a named variable within this evaluation context.
	 * @param name variable to lookup
	 * @return the value of the variable, or {@code null} if not found
	 */
	@Nullable
	Object lookupVariable(String name);

}

默认实现是StandardEvaluationContext类,使用setRootObject方法来设置根对象,使用setVariable方法来注册自定义变量,使用registerFunction来注册自定义函数等。

SpringExpressions.g文件,.g后缀名文件,采用Glagol DSL语法写的配置文件。

@Value可以配合SpEL表达式一起使用,读取properties文件内容:
@Value("#{configProperties['ora_driver']}")

SpEL and @Value

参考

SpEL基础

原创文章 131 获赞 175 访问量 32万+

猜你喜欢

转载自blog.csdn.net/lonelymanontheway/article/details/105786501