這篇文章主要介紹如何使用Spring Expression Language,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
是強大的表達式語言,支持查詢、操作運行時對象圖,以及解析邏輯、算術表達式。SpEL可以獨立使用,無論你是否使用Spring框架。
引入依賴:
compile group: 'org.springframework', name: 'spring-expression', version: '5.2.4.RELEASE'
讀者可以選擇最新版本或合適的版本。當然也可以下載相應jar文件。在調用下面的函數之前,按如下方式初始化一個類級屬性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.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);
}這里直接解決字符串及數字文本。
2.2. 直接文本上調用方法
/**
* 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);
}示例展示了在字符串上直接調用Java String類的public方法。
2.3.訪問對象屬性和方法
/**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);
}表達式可以直接使用對象的屬性與方法。我們看到方法與屬性使用一樣,只是多了調用括號。
2.4.執行各種操作(比較、邏輯、算術)
SpEl支持下面幾種操作:
關系比較操作:==, !=, <, <=, >, >=
邏輯操作: and, or, not
算術操作: +, -, /, *, %, ^
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.使用多個對象和變量
表達式不僅需要引用對象,而且可能需要引用多個不同類型的對象。我們可以把所有使用的對象都加入至上下文中。使用鍵值對的方式加入并引用。
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.調用自定義函數
SpEl也可以調用自定義的函數,用戶可以擴展業務邏輯。下面首先定義一個函數:
public class StringHelper {
public static boolean isValid(String url){
return true;
}
}下面在SpEl中調用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();
}
}通過示例介紹了SpEl中多種應用場景。讀者可以利用這些功能實現更加靈活的功能應用。
Spring 表達式語言(簡稱SpEL):是一個支持運行時查詢和操作對象圖的強大的表達式語言。
語法類似于 EL:SpEL 使用 #{…} 作為定界符,所有在大框號中的字符都將被認為是 SpEL
SpEL 為 bean 的屬性進行動態賦值提供了便利.
通過 SpEL 可以實現:
通過 bean 的 id 對 bean 進行引用
調用方法以及引用對象中的屬性
計算表達式的值
正則表達式的匹配
字面量的表示:
整數:
<property name="count" value="#{5}"/>
小數:
<property name="frequency" value="#{89.7}"/>
科學計數法:
<property name="capacity" value="#{1e4}"/>
String可以使用單引號或者雙引號作為字符串的定界符號:
<property name=“name” value="#{'Chuck'}"/>
或
<property name='name' value='#{"Chuck"}'/>
Boolean:
<property name="enabled" value="#{false}"/>如果僅僅是表示字面量,其實是沒有必要使用Spring EL表達式的,這里僅僅演示一下而已,日常的開發中很少使用。
引用其他對象

但是我們更常用ref 來實現其他對象的引用
引用其他對象的屬性

調用其他方法,還可以鏈式操作


調用靜態方法或靜態屬性
通過 T() 調用一個類的靜態方法,它將返回一個 Class Object,然后再調用相應的方法或屬性:

算數運算符:+, -, *, /, %, ^

加號還可以用作字符串連接

比較運算符: <, >, ==, <=, >=, lt, gt, eq, le, ge


邏輯運算符號: and, or, not, |

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

if-else 的變體

正則表達式:matches


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: 車輛
*
* @author: Mr.Yang
*
* @date: 2018年4月7日 下午8:30:01
*/
public class Car {
private String brand;
private double price;
// 調用靜態方法或靜態屬性:通過 T() 調用一個類的靜態方法,它將返回一個 Class Object,然后再調用相應的方法或屬性
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;
// 通過 Spring El 引用 Address的city
private String city;
// 通過 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}" />
<!-- 通過Spring El表達式為屬性賦值一個字面值 ,
當然了,如果是字面值就沒有必要使用Spring El表達式了,這里僅僅是演示該用法 -->
<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>測試類:
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);
}
}結果:
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]
示例-基于注解的方式
我們通過一個數據庫的例子來演示。雖然可以通過Spring El 表達式從配置文件中加載一個參數值,比如
@Value("#{properties['jdbc.driverClassName']}")是不是容易出錯…. Spring提供了更好的方式 context:property-placeholder。

package com.xgj.spel.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*
*
* @ClassName: MyDataSource
*
* @Description: 數據源 @Component標注
*
* @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注解自動注入屬性配置文件中對應屬性的值
*
* @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());
}
}運行結果
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
以上是“如何使用Spring Expression Language”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。