使用Spring Expression Language (SpEL)全面解析表達(dá)式
Spring Expression Language (SpEL)
是強(qiáng)大的表達(dá)式語(yǔ)言,支持查詢、操作運(yùn)行時(shí)對(duì)象圖,以及解析邏輯、算術(shù)表達(dá)式。SpEL可以獨(dú)立使用,無(wú)論你是否使用Spring框架。
本文嘗試通過(guò)多個(gè)示例使用SpEL,探索其強(qiáng)大能力。
1.環(huán)境準(zhǔn)備
引入依賴:
compile group: 'org.springframework', name: 'spring-expression', version: '5.2.4.RELEASE'
讀者可以選擇最新版本或合適的版本。當(dāng)然也可以下載相應(yīng)jar文件。在調(diào)用下面的函數(shù)之前,按如下方式初始化一個(gè)類(lèi)級(jí)屬性SpelExpression解析器:
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class ElMain {
private ExpressionParser parser;
ElMain(){
parser = new SpelExpressionParser();
}
public static void main(String[] args) {
ElMain elHelper = new ElMain();
elHelper.evaluateLiteralExpresssions();
}
private static void print(Object message){
System.out.println(message);
}
2.SpEL示例應(yīng)用
2.1. 解析直接文本
private void evaluateLiteralExpresssions() {
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();
print(message);
exp = parser.parseExpression("6");
Integer value = exp.getValue(Integer.class);
print(value*2);
}
這里直接解決字符串及數(shù)字文本。
2.2. 直接文本上調(diào)用方法
? ? /**
? ? ?* A function that tests method invocation on literals
? ? ?*/
? ? private void methodInvocationOnLiterals() {
? ? ? ? Expression exp = parser.parseExpression("'Hello World'.concat('!')");
? ? ? ? String message = (String) exp.getValue();
? ? ? ? println(message);
? ? ? ? exp = parser.parseExpression("'Hello World'.length()");
? ? ? ? Integer size = exp.getValue(Integer.class);
? ? ? ? println(size);
? ? ? ? exp = parser.parseExpression("'Hello World'.split(' ')[0]");
? ? ? ? message = (String)exp.getValue();
? ? ? ? println(message);
? ? }示例展示了在字符串上直接調(diào)用Java String類(lèi)的public方法。
2.3.訪問(wèn)對(duì)象屬性和方法
? ? /**A function that tests accessing properties of objects**/
? ? private void accessingObjectProperties() {
? ? ? ? User user = new User("John", "Doe", ?true, "john.doe@acme.com",30);
? ? ? ? Expression exp = parser.parseExpression("firstName");
? ? ? ? println((String)exp.getValue(user));
? ? ? ? exp = parser.parseExpression("isAdmin()==false");
? ? ? ? boolean isAdmin = exp.getValue(user, Boolean.class);
? ? ? ? println(isAdmin);
? ? ? ? exp = parser.parseExpression("email.split('@')[0]");
? ? ? ? String emailId = exp.getValue(user, String.class);
? ? ? ? println(emailId);
? ? ? ? exp = parser.parseExpression("age");
? ? ? ? Integer age = exp.getValue(user, Integer.class);
? ? ? ? println(age);
? ? }表達(dá)式可以直接使用對(duì)象的屬性與方法。我們看到方法與屬性使用一樣,只是多了調(diào)用括號(hào)。
2.4.執(zhí)行各種操作(比較、邏輯、算術(shù))
SpEl支持下面幾種操作:
- 關(guān)系比較操作:==, !=, <, <=, >, >=
- 邏輯操作: and, or, not
- 算術(shù)操作: +, -, /, *, %, ^
? ? private void operators() {
? ? ? ? User user = new User("John", "Doe", true,"john.doe@acme.com", ?30);
? ? ? ? Expression exp = parser.parseExpression("age > 18");
? ? ? ? println(exp.getValue(user,Boolean.class));
? ? ? ? exp = parser.parseExpression("age < 18 and isAdmin()");
? ? ? ? println(exp.getValue(user,Boolean.class));
? ? }2.5.使用多個(gè)對(duì)象和變量
表達(dá)式不僅需要引用對(duì)象,而且可能需要引用多個(gè)不同類(lèi)型的對(duì)象。我們可以把所有使用的對(duì)象都加入至上下文中。使用鍵值對(duì)的方式加入并引用。
? ? private void variables() {
? ? ? ? User user = new User("John", "Doe", ?true, "john.doe@acme.com",30);
? ? ? ? Application app = new Application("Facebook", false);
? ? ? ? StandardEvaluationContext context = new StandardEvaluationContext();
? ? ? ? context.setVariable("user", user);
? ? ? ? context.setVariable("app", app);
? ? ? ? Expression exp = parser.parseExpression("#user.isAdmin() and #app.isActive()");
? ? ? ? Boolean result = exp.getValue(context,Boolean.class);
? ? ? ? println(result);
? ? }2.6.調(diào)用自定義函數(shù)
SpEl也可以調(diào)用自定義的函數(shù),用戶可以擴(kuò)展業(yè)務(wù)邏輯。下面首先定義一個(gè)函數(shù):
public class StringHelper {
? ? public static boolean isValid(String url){
? ? ? ? return true;
? ? }
}下面在SpEl中調(diào)用isValid方法:
? ? private void customFunctions() {
? ? ? ? try {
? ? ? ? ? ? StandardEvaluationContext context = new StandardEvaluationContext();
? ? ? ? ? ? context.registerFunction("isURLValid",
? ? ? ? ? ? ? ? ? ? StringHelper.class.getDeclaredMethod("isValid", new Class[] { String.class }));
? ? ? ? ? ? String expression = "#isURLValid('http://google.com')";
? ? ? ? ? ? Boolean isValid = parser.parseExpression(expression).getValue(context, Boolean.class);
? ? ? ? ? ? println(isValid);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }3.小結(jié)
通過(guò)示例介紹了SpEl中多種應(yīng)用場(chǎng)景。讀者可以利用這些功能實(shí)現(xiàn)更加靈活的功能應(yīng)用。
Spring表達(dá)式語(yǔ)言SpEL
Spring 表達(dá)式語(yǔ)言(簡(jiǎn)稱SpEL):是一個(gè)支持運(yùn)行時(shí)查詢和操作對(duì)象圖的強(qiáng)大的表達(dá)式語(yǔ)言。
語(yǔ)法類(lèi)似于 EL:SpEL 使用 #{…} 作為定界符,所有在大框號(hào)中的字符都將被認(rèn)為是 SpEL
SpEL 為 bean 的屬性進(jìn)行動(dòng)態(tài)賦值提供了便利.
通過(guò) SpEL 可以實(shí)現(xiàn):
- 通過(guò) bean 的 id 對(duì) bean 進(jìn)行引用
- 調(diào)用方法以及引用對(duì)象中的屬性
- 計(jì)算表達(dá)式的值
- 正則表達(dá)式的匹配
SpEL:字面量
字面量的表示:
整數(shù):
<property name="count" value="#{5}"/>
小數(shù):
<property name="frequency" value="#{89.7}"/>
科學(xué)計(jì)數(shù)法:
<property name="capacity" value="#{1e4}"/>
String可以使用單引號(hào)或者雙引號(hào)作為字符串的定界符號(hào):
<property name=“name” value="#{'Chuck'}"/>
或
<property name='name' value='#{"Chuck"}'/>
Boolean:
<property name="enabled" value="#{false}"/>
如果僅僅是表示字面量,其實(shí)是沒(méi)有必要使用Spring EL表達(dá)式的,這里僅僅演示一下而已,日常的開(kāi)發(fā)中很少使用。
SpEL:引用 Bean、屬性和方法
引用其他對(duì)象

但是我們更常用ref 來(lái)實(shí)現(xiàn)其他對(duì)象的引用
引用其他對(duì)象的屬性

調(diào)用其他方法,還可以鏈?zhǔn)讲僮?/strong>


調(diào)用靜態(tài)方法或靜態(tài)屬性
通過(guò) T() 調(diào)用一個(gè)類(lèi)的靜態(tài)方法,它將返回一個(gè) Class Object,然后再調(diào)用相應(yīng)的方法或?qū)傩裕?/p>

SpEL支持的運(yùn)算符號(hào)
算數(shù)運(yùn)算符:+, -, *, /, %, ^

加號(hào)還可以用作字符串連接

比較運(yùn)算符: <, >, ==, <=, >=, lt, gt, eq, le, ge


邏輯運(yùn)算符號(hào): and, or, not, |

if-else 運(yùn)算符:?: (ternary), ?: (Elvis)

if-else 的變體

正則表達(dá)式:matches

示例-基于xml的方式

package com.xgj.spel;
/**
*
*
* @ClassName: Address
*
* @Description: 地址信息
*
* @author: Mr.Yang
*
* @date: 2018年4月7日 下午8:29:12
*/
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
}
package com.xgj.spel;
/**
*
*
* @ClassName: Car
*
* @Description: 車(chē)輛
*
* @author: Mr.Yang
*
* @date: 2018年4月7日 下午8:30:01
*/
public class Car {
private String brand;
private double price;
// 調(diào)用靜態(tài)方法或靜態(tài)屬性:通過(guò) T() 調(diào)用一個(gè)類(lèi)的靜態(tài)方法,它將返回一個(gè) Class Object,然后再調(diào)用相應(yīng)的方法或?qū)傩?
private long weight;
public long getWeight() {
return weight;
}
public void setWeight(long weight) {
this.weight = weight;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", weight=" + weight + "]";
}
}
package com.xgj.spel;
public class Boss {
private String name;
private Car car;
// 通過(guò) Spring El 引用 Address的city
private String city;
// 通過(guò) Car的price屬性,確定info ,如果car.price>=500000 ,info 為CEO,否則為 Staff
private String info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Boss [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]";
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.xgj.spel.Car"
p:brand="Bench"
p:price="700000"
p:weight="#{T(java.lang.Math).PI * 4567}" />
<!-- 通過(guò)Spring El表達(dá)式為屬性賦值一個(gè)字面值 ,
當(dāng)然了,如果是字面值就沒(méi)有必要使用Spring El表達(dá)式了,這里僅僅是演示該用法 -->
<bean id="address" class="com.xgj.spel.Address"
p:city="#{'NanJing'}"
p:street="RuanJianDaDao" />
<bean id="boss" class="com.xgj.spel.Boss"
p:name="Artisan"
p:city="#{address.city}"
p:car-ref="car"
p:info="#{car.price > 500000 ? 'CEO' : 'staff'}" />
</beans>
測(cè)試類(lèi):
package com.xgj.spel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpelTest {
public static void main(String[] args) {
String configLocation = "com/xgj/spel/beans_spel.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
Car car = (Car) ctx.getBean("car");
System.out.println(car);
Boss boss = (Boss) ctx.getBean("boss");
System.out.println(boss);
}
}
結(jié)果:
2018-04-07 21:21:30,804 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4af6178d: startup date [Sat Apr 07 21:21:30 BOT 2018]; root of context hierarchy
2018-04-07 21:21:30,907 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/spel/beans_spel.xml]
Car [brand=Bench, price=700000.0, weight=14347]
Boss [name=Artisan, car=Car [brand=Bench, price=700000.0, weight=14347], city=NanJing, info=CEO]
示例-基于注解的方式
我們通過(guò)一個(gè)數(shù)據(jù)庫(kù)的例子來(lái)演示。雖然可以通過(guò)Spring El 表達(dá)式從配置文件中加載一個(gè)參數(shù)值,比如
@Value("#{properties['jdbc.driverClassName']}")是不是容易出錯(cuò)…. Spring提供了更好的方式 context:property-placeholder。

package com.xgj.spel.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*
*
* @ClassName: MyDataSource
*
* @Description: 數(shù)據(jù)源 @Component標(biāo)注
*
* @author: Mr.Yang
*
* @date: 2018年4月7日 下午9:26:32
*/
@Component
public class MyDataSource {
private String driverClass;
private String url;
private String username;
private String password;
public String getDriverClass() {
return driverClass;
}
/**
*
*
* @Title: setDriverClass
*
* @Description: @Value注解自動(dòng)注入屬性配置文件中對(duì)應(yīng)屬性的值
*
* @param driverClass
*
* @return: void
*/
@Value("${jdbc.driverClassName}")
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
public String getUrl() {
return url;
}
@Value("${jdbc.url}")
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
// @Value("$(jdbc.username)")
@Value("${jdbc.username}")
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
@Value("${jdbc.password}")
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "MyDataSource [driverClass=" + driverClass + ", url=" + url + ", username=" + username + ", password=" + password + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 掃描的基包 -->
<context:component-scan base-package="com.xgj.spel.annotation"/>
<!-- 加載外部properties文件 -->
<context:property-placeholder location="classpath:mysql/db_mysql.properties"/>
</beans>
db_mysql.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/artisan jdbc.username=artisan jdbc.password=artisan
package com.xgj.spel.annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCase {
@Test
public void test() {
String configurationLocation = "com/xgj/spel/annotation/beans_anno.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configurationLocation);
MyDataSource myDataSource = (MyDataSource) ctx.getBean("myDataSource");
System.out.println(myDataSource);
System.out.println("driverClassName:" + myDataSource.getDriverClass());
System.out.println("url:" + myDataSource.getUrl());
System.out.println("username:" + myDataSource.getUsername());
System.out.println("password:" + myDataSource.getPassword());
}
}
運(yùn)行結(jié)果
2018-04-07 23:37:11,409 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@761df304: startup date [Sat Apr 07 23:37:11 BOT 2018]; root of context hierarchy
2018-04-07 23:37:11,552 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/spel/annotation/beans_anno.xml]
MyDataSource [driverClass=com.mysql.jdbc.Driver, url=jdbc:mysql://localhost:3306/artisan, username=artisan, password=artisan]
driverClassName:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/artisan
username:artisan
password:artisan
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置
這篇文章主要介紹了SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
開(kāi)源項(xiàng)目ERM模型轉(zhuǎn)jpa實(shí)體maven插件使用
這篇文章主要為大家介紹了開(kāi)源項(xiàng)目ERM模型轉(zhuǎn)jpa實(shí)體maven插件的使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
SpringSecurity?表單登錄的實(shí)現(xiàn)
本文主要介紹了SpringSecurity?表單登錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
基于Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié)
這篇文章主要介紹了Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Mybatis分頁(yè)插件PageHelper配置及使用方法詳解
這篇文章主要介紹了Mybatis分頁(yè)插件PageHelper配置及使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

