`
thaIm
  • 浏览: 91192 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring --- SpEL

阅读更多
一)什么是SpEL
  SpEL -- Spring Expression Language. Spring的表达式语言。举个最简单的例子:
ExpressionParser parser =new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();

    最后 message的值就是 Hello World, 表达式中的单引号''就是表达String类型的一种格式。另外值得注意的一点时,当表达式不符合规范时, parser.parseExpression语句会抛出ParseException;而exp.getValue会抛出EvaluationException
    而为了避免最后的类型转换,我们还可以这样写:
String message = exp.getValue(String.class);


二)SpEL举例中需要使用到的类
    以下是本文介绍SpEL过程中需要使用到的类,在此列出,以供后续介绍中使用参考:
Inventor.java
package org.spring.samples.spel.inventor;
import java.util.Date;
import java.util.GregorianCalendar;

public class Inventor {
  private String name;
  private String nationality;
  private String[] inventions;
  private Date birthdate;
  private PlaceOfBirth placeOfBirth;

  public Inventor(String name, String nationality){
    GregorianCalendar c= new GregorianCalendar();
    this.name = name;
    this.nationality = nationality;
    this.birthdate = c.getTime();
  }
  public Inventor(String name, Date birthdate, String nationality) {
    this.name = name;
    this.nationality = nationality;
    this.birthdate = birthdate;
  }
  public Inventor() {}

  public String getName() { return name;}
  public void setName(String name) { this.name = name;}

  public String getNationality() { return nationality;}
  public void setNationality(String nationality) { this.nationality = nationality; }

  public Date getBirthdate() { return birthdate;}
  public void setBirthdate(Date birthdate) { this.birthdate = birthdate;}

  public PlaceOfBirth getPlaceOfBirth() { return placeOfBirth;}
  public void setPlaceOfBirth(PlaceOfBirth placeOfBirth) { this.placeOfBirth = placeOfBirth;}

  public void setInventions(String[] inventions) { this.inventions = inventions; }
  public String[] getInventions() { return inventions;}
}

PlaceOfBirth.java
package org.spring.samples.spel.inventor;

public class PlaceOfBirth {
  private String city;
  private String country;

  public PlaceOfBirth(String city) {this.city=city;}
  public PlaceOfBirth(String city, String country){
    this(city);
    this.country = country;
  }

  public String getCity() {return city;}
  public void setCity(String s) {this.city = s;}

  public String getCountry() {return country;}
  public void setCountry(String country) {this.country = country;}
}

Society.java
package org.spring.samples.spel.inventor;
import java.util.*;

public class Society {
  private String name;
  public static String Advisors = "advisors";
  public static String President = "president";
  private List<Inventor> members = new ArrayList<Inventor>();
  private Map officers = new HashMap();

  public List getMembers() {return members;}
  public Map getOfficers() {return officers;}
  public String getName() {return name;}
  public void setName(String name) {this.name = name;}
  public boolean isMember(String name){
    boolean found = false;
    for (Inventor inventor : members) {
      if (inventor.getName().equals(name)){
        found = true;
        break;
      }
    }
    return found;
  }
}


三)EvaluationContext接口
    当在使用表达式的过程中有遇到类型转换时,我们需要EvaluationContext接口加以辅助:
class Simple {
  public List<Boolean> booleanList = new ArrayList<Boolean>();
}

Simple simple = new Simple();
simple.booleanList.add(true);
EvaluationContext simpleContext = new StandardEvaluationContext(simple);
// false is passed in here as a string. SpEL and the conversion service will
// correctly recognize that it needs to be a Boolean and convert it
parser.parseExpression("booleanList[0]").setValue(simpleContext, "false");
// b will be false
Boolean b = simple.booleanList.get(0);

注:EvaluationContext开销较大,所以多用于数据变化较少的情况。如果数据变化频繁,我们可以考虑直接引用对象(比如上例中的simple)以减小开销。

四)SpEL
  下面正式开始介绍SpEL。
1'Literal expressions 基本类的表达式
ExpressionParser parser = new SpelExpressionParser();
// evals to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();

// evals to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
Object nullValue = parser.parseExpression("null").getValue();


2' Properties, Arrays, Lists, Maps, Indexers 属性、集合的表示
    Properties:
// evals to 1856
int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);

    arrays and lists:
ExpressionParser parser = new SpelExpressionParser();
// Inventions Array
StandardEvaluationContext teslaContext = new StandardEvaluationContext(tesla);
// evaluates to "Induction motor"
String invention = parser.parseExpression("inventions[3]").getValue(teslaContext,String.class);

// Members List
StandardEvaluationContext societyContext = new StandardEvaluationContext(ieee);
// evaluates to "Nikola Tesla"
String name = parser.parseExpression("Members[0].Name").getValue(societyContext, String.class);

// List and Array navigation
// evaluates to "Wireless communication"
String invention = parser.parseExpression("Members[0].Inventions[6]").getValue(societyContext,String.class);

   Map:
// Officer's Dictionary
Inventor pupin = parser.parseExpression("Officers['president']").getValue(societyContext,Inventor.class);
// evaluates to "Idvor"
String city =
parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(societyContext,String.class);
// setting values
parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext,"Croatia");


3' construction
    lists:
// evaluates to a Java list containing the four numbers
List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context);
List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context);

    arrays:
int[] numbers1 = (int[]) parser.parseExpression("new int[4]").getValue(context);
// Array with initializer
int[] numbers2 = (int[]) parser.parseExpression("new int[]{1,2,3}").getValue(context);
// Multi dimensional array
int[][] numbers3 = (int[][]) parser.parseExpression("new int[4][5]").getValue(context);


4' Methods
// string literal, evaluates to "bc"
String c = parser.parseExpression("'abc'.substring(2, 3)").getValue(String.class);
// evaluates to true
boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(societyContext,
Boolean.class);


5' Operators
    Relational operators:
// evaluates to true
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
// evaluates to false
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
// evaluates to true
boolean trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);
// evaluates to false
boolean falseValue = parser.parseExpression("'xyz' instanceof T(int)").getValue(Boolean.class);
// evaluates to true
boolean trueValue =
parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
//evaluates to false
boolean falseValue =
parser.parseExpression("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

注意关键字:instanceof、matches

    Logical operators:
// evaluates to false
boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);
// evaluates to true
String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
// -- OR -//
evaluates to true
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);
// evaluates to true
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstien')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
// -- NOT -//
evaluates to false
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);
// -- AND and NOT -String
expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";
boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);


    Mathematical operators:
// Addition
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
String testString =
parser.parseExpression("'test' + ' ' + 'string'").getValue(String.class); // 'test string'
// Subtraction
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000
// Multiplication
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0
// Division
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0
// Modulus
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1
// Operator precedence
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21


6' Assignment
Inventor inventor = new Inventor();
StandardEvaluationContext inventorContext = new StandardEvaluationContext(inventor);
parser.parseExpression("Name").setValue(inventorContext, "Alexander Seovic2");
// alternatively
String aleks = parser.parseExpression("Name = 'Alexandar Seovic'").getValue(inventorContext,String.class);


7' Types
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);

注: 在java.lang中的类不需要全路径。

8' Constructors
Inventor einstein =
p.parseExpression("new org.spring.samples.spel.inventor.Inventor('Albert Einstein','German')").getValue(Inventor.class);
//create new inventor instance within add method of List
p.parseExpression("Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein','German'))").getValue(societyContext);


9' Variables
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"

注: Spring还有两个预留的variables: #this #root   #this表示集合中的当前元素,#root表示根对象,即载入context的对象。

10' Functions
    Function的使用略微复杂。首先是调用StandardEvaluationContext的方法
public void registerFunction(String name, Method m)
进行注册,而后才能在expression中进行调用:
public abstract class StringUtils {
  public static String reverseString(String input) {
    StringBuilder backwards = new StringBuilder();
    for (int i = 0; i < input.length(); i++)
      backwards.append(input.charAt(input.length() - 1 - i));
    }
    return backwards.toString();
  }
}


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);


11'Bean references
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());
// This will end up calling resolve(context,"foo") on MyBeanResolver during evaluation
Object bean = parser.parseExpression("@foo").getValue(context);

没啥好多说的,知道用@就可以了~~

12'Ternary Operator
String falseString = parser.parseExpression("false ? 'trueExp' : 'falseExp'").getValue(String.class);

结果是: falseExp

13'The Elvis Operator
这是一种对三重操作符的简化写法。比如用三重操作符我们可以这样:
String name = "Elvis Presley";
String displayName = name != null ? name : "Unknown";

等效的用Elvis操作符实现的话就是这样:
ExpressionParser parser = new SpelExpressionParser();
String name = parser.parseExpression("null?:'Unknown'").getValue(String.class);
System.out.println(name); // 'Unknown'

省略了name的显示表达!

14'Safe Navigation operator
用?.取代. 当代码会抛出NullPointerException时,采用?.的操作仅仅会简单的返回一个null。
ExpressionParser parser = new SpelExpressionParser();
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
String city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);
System.out.println(city); // Smiljan
tesla.setPlaceOfBirth(null);
city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);
System.out.println(city); // null - does not throw NullPointerException!!!


15' Collection Selection
如果你要在集合内挑选符合某些条件的元素,你可以采用这样格式的表达式?[selectionExpression]
List<Inventor> list = (List<Inventor>)
parser.parseExpression("Members.?[Nationality == 'Serbian']").getValue(societyContext);
Map newMap = parser.parseExpression("map.?[value<27]").getValue();

而如果你只需要获得第一个满足条件的元素,你可以用:^[...]
而如果你只需要获得最后一个满足条件的元素,你可以用:$[...]

16' Collection Projection
那么我需要便利某些元素以组成新的集合,我们可以采用这样的表达式![projectionExpression]:
// returns [ 'Smiljan', 'Idvor' ]
List placesOfBirth = (List)parser.parseExpression("Members.![placeOfBirth.city]");


17' Expression templating
   template的作用便是在表达式内再嵌套表达式。比如:
String randomPhrase =
parser.parseExpression("random number is #{T(java.lang.Math).random()}",new TemplateParserContext()).getValue(String.class);
// evaluates to "random number is 0.7038186818312008"

而要实现内部嵌套的T(java.lang.Math).random()表达式。我们需要实现接口ParserContext:
public class TemplateParserContext implements ParserContext {
  public String getExpressionPrefix() {
    return "#{";
  }
  public String getExpressionSuffix() {
    return "}";
  }
  public boolean isTemplate() {
    return true;
  }
}


五)SpEL在配置中的使用
  同样的语法,SpEL还能在配置文件和annotation中使用!!强大吧 ^.^ 它们的格式是#{ <expression string> }
  在配置文件中:
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
  <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
  <!-- other properties -->
</bean>
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
  <property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
  <!-- other properties -->
</bean>
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
  <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
  <!-- other properties -->
</bean>
<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
  <property name="initialShapeSeed" value="#{numberGuess.randomNumber }"/>
  <!-- other properties -->
</bean>

  在annotation中使用:
public static class FieldValueTestBean{
  @Value("#{ systemProperties['user.region'] }")
  private String defaultLocale;
分享到:
评论

相关推荐

    jasypt-spring-boot-starter 3.0.5依赖的pom及jar

    3. **Spring Expression Language (SpEL) 支持**:可以在Spring的表达式语言中直接使用加密值,简化了加密数据的使用。 4. **易于集成**:与其他Spring Boot组件无缝集成,如Spring Security,使得加密操作与整个...

    SpringCloud Function SpEL注入漏洞分析(CVE-2022-22963).doc

    SpringCloud Function SpEL 注入漏洞分析(CVE-2022-22963) SpringCloud Function 是 Spring 提供的一套分布式函数式编程组件,旨在帮助开发者专注于业务逻辑实现,而不需要关心服务器环境运维等问题。然而,在 ...

    Spring框架(spring-framework-5.2.6.RELEASE)的jar包

    - `spring-expression-5.2.6.RELEASE.jar`:Spring表达式语言(SpEL),用于在运行时查询和操作对象图。 - `spring-jdbc-5.2.6.RELEASE.jar`:简化了JDBC的使用。 - `spring-tx-5.2.6.RELEASE.jar`:事务管理服务,...

    官方原版完整包 spring-framework-5.3.6.RELEASE-dist.zip

    此外,这个包可能还包括其他相关组件,如Spring Expression Language (SpEL) 和Spring Test,用于测试和配置Spring应用。 2. spring-5.3.6-docs.zip:这个文件提供了Spring Framework 5.3.6.RELEASE的官方文档。...

    官方原版完整包 spring-framework-5.3.1.RELEASE.zip

    7. **spring-expression**: 提供了一个强大的表达式语言(SPeL),用于在运行时查询和操作对象图。 **spring-5.3.1-docs.zip** 包含了Spring Framework 5.3.1的官方文档。这些文档包括用户指南、参考手册、API文档...

    spring-framework-5.1.8.RELEASE.zip

    1. **核心容器**:由`spring-core`, `spring-beans`, `spring-context`, `spring-expression`等jar组成,它们定义了Spring的基本概念,如Bean工厂和应用上下文,以及表达式语言(SpEL)。 2. **数据访问/集成**:...

    spring4源码所需要的spring-objenesis-repack-2.1.jar,spring-cglib-repack-3.1.jar

    3. **Spring Core**: `spring-core-4.1.3.BUILD-SNAPSHOT.jar` 是Spring框架的核心组件,包含了Spring的核心功能,如依赖注入(DI)、资源管理、类型转换系统和SpEL(Spring Expression Language)。这些组件是...

    spring-framework-4.3.6.RELEASE-dist.zip

    1. **Spring Core Container**:这是Spring框架的基础,包括Core、 Beans、Context和SpEL(Spring Expression Language)模块。Core模块提供了基本的IoC(Inversion of Control)容器,管理对象的生命周期和依赖关系...

    spring-framework-4.2.4 所有jar包和xsd文件

    5. `spring-expression.jar`:Spring表达式语言(SpEL),用于在运行时查询和操作对象图。 6. `spring-web.jar` 和 `spring-webmvc.jar`:为Web应用提供支持,包括HTTP多部分解析、Servlet监听器和Spring MVC框架。 ...

    spring-framework-4.2.4.RELEASE-dist Spring资源包

    5. `spring-expression`(SpEL):Spring表达式语言,用于在运行时查询和操作对象图。 6. `spring-web`和`spring-webmvc`模块:支持Web应用的开发,包括HTTP请求处理和MVC框架。 7. `spring-jdbc`和`spring-orm`模块...

    spring-framework-3.0.RELEASE

    5. **spring-expression**:Spring表达式语言(SpEL),用于在运行时查询和操作对象图。 6. **spring-web**:支持Web应用,包括HTTP请求处理、MVC框架等。 7. **spring-webmvc**:Spring MVC模块,用于构建Web应用...

    spring-core3.2.5集合包

    3. **spring-expression**(SpEL):Spring 表达式语言,用于在运行时查询和操作对象图。 4. **spring-core**:核心工具类,包括反射、类型转换、异常处理等基础功能。 5. **spring-aop**:实现了 AOP 功能,包括...

    spring-boot-annotation-spel.zip

    Spring Boot自定义注解配合SPEL(Spring Expression Language)表达式可以让我们实现更为灵活的逻辑控制。本压缩包“spring-boot-annotation-spel.zip”显然是关于如何在自定义注解中运用SPEL表达式的实例教程。 ...

    spring最新版本4.3.3所有jar包

    5. **spring-expression.jar (SpEL)**:Spring表达式语言,用于在运行时查询和操作对象图,支持在配置元数据中使用表达式。 6. **spring-web.jar**:为Web应用提供了支持,包括HTTP请求处理、Servlet监听器和MVC...

    dynamic-datasource-spring-boot-starter-v3.5.1.zip

    Dynamic-Datasource (opens new window)- 基于 SpringBoot 的多数据源组件,功能强悍,支持 Seata ...提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。 支持 多层数据源嵌套切换 。

    spring-expression-3.2.1.RELEASE.zip

    标题中的"spring-expression-3.2.1.RELEASE.zip"是一个Spring框架的组件,Spring Expression Language (SpEL) 的特定版本。SpEL是Spring框架的一部分,它提供了一种强大的表达式语言,用于在运行时查询和操作对象图...

    spring-context源码

    `spring-context`包含了强大的Spring表达式语言(SpEL),它允许在运行时对bean和属性进行动态查询和修改。SpEL可以用在注解的值、bean的属性绑定以及`ExpressionEvaluator`的表达式解析等多个场景。 综上所述,`...

    官方源码 spring-framework-5.3.3.zip

    5. `spring-expression`(SpEL):强大的表达式语言,用于在运行时查询和操作对象图。 6. `spring-web`:Web层的支持,包括Servlet监听器、MVC框架等。 7. `spring-webmvc`:Spring的Web MVC实现,提供模型-视图-...

    spring需要导入的包(4.3.7)

    `spring-expression.jar`(SpEL)提供了强大的表达式语言,用于在运行时查询和操作对象图。 对于AOP支持,我们需要`spring-aop.jar`,它实现了面向切面编程,允许我们定义拦截器、通知和切面,实现代码的解耦。同时...

    spring-framework-4.2.0.RELEASE官方jar包

    - **Spring Expression Language (SpEL)**:在 4.2 版本中,SpEL 进行了优化,提供了更强大和灵活的表达式语言,用于在运行时查询和操作对象图。 这个官方jar包包含了所有这些模块的实现,包括但不限于 spring-core...

Global site tag (gtag.js) - Google Analytics