基于springEL表達式詳解及應用
基于springEL表達式詳解及應用
什么是SpringEL?
Spring3中引入了Spring表達式語言—SpringEL,SpEL是一種強大,簡潔的裝配Bean的方式,他可以通過運行期間執(zhí)行的表達式將值裝配到我們的屬性或構(gòu)造函數(shù)當中,更可以調(diào)用JDK中提供的靜態(tài)常量,獲取外部Properties文件中的的配置
為什么要使用SpringEL?
我們平常通過配置文件或Annotaton注入的Bean,其實都可以稱為靜態(tài)性注入,試想一下,若然我Bean A中有變量A,它的值需要根據(jù)Bean B的B變量為參考,在這場景下靜態(tài)注入就對這樣的處理顯得非常無力,而Spring3增加的SpringEL就可以完全滿足這種需求,而且還可以對不同Bean的字段進行計算再進行賦值,功能非常強大
如何使用SpringEL?
SpringEL從名字來看就能看出,和EL是有點關系的,SpringEL的使用和EL表達式的使用非常相似,EL表達式在JSP頁面更方便的獲取后臺中的值,而SpringEL就是為了更方便獲取Spring容器中的Bean的值,EL使用${},而SpringEL使用#{}進行表達式的聲明。
使用SpringEL注入簡單值
public class TestSpringEL {
/*
* @Value注解等同于XML配置中的<property/>標簽,
* SpringEL同樣支持在XML<property/>中編寫
*/
// 注入簡單值,輸出num為5
@Value("#{5}")
private Integer num;
// 注入ID為testConstant的Bean
@Value("#{testConstant}")
private TestConstant Constant;
// 注入ID為testConstant Bean中的STR常量/變量
@Value("#{testConstant.STR}")
private String str;
}
使用SpringEL調(diào)用方法
public class TestSpringEL {
/*
* TestConstant類中有兩個方法重載,
* 返回值為String類型
*/
// 調(diào)用無參方法
@Value("#{testConstant.showProperty}")
private String method1;
// 有參接收字符串的方法
@Value("#{testConstant.showProperty('Hello')}")
private String method2;
/*
* 若然希望方法返回的String為大寫
*/
@Value("#{testConstant.showProperty().toUpperCase()}")
private String method3;
/*
* 若使用method3這種方式,若然showProperty返回為null,
* 將會拋出NullPointerException,可以使用以下方式避免
*/
@Value("#{testConstant.showProperty()?.toUpperCase}")
private String method4;
/*
* 使用?.符號代表若然左邊的值為null,將不執(zhí)行右邊方法,
* 讀者可以靈活運用在其他場景,只要左邊可能返回null,
* 即可使用上面示例中的?.
*/
}
SpringEL調(diào)用靜態(tài)類或常量
public class TestSpringEL {
/*
* 注入JDK中的工具類常量或調(diào)用工具類的方法
*/
// 獲取Math的PI常量
@Value("#{T(java.lang.Math).PI")
private double pi;
// 調(diào)用random方法獲取返回值
@Value("#{T(java.lang.Math).random()}")
private double ramdom;
// 獲取文件路徑符號
@Value("#{T(java.io.File).separator}")
private String separator;
}
SpringEL運算
public class TestSpringEL {
/*
* 使用SpringEL進行運算及邏輯操作
*/
// 拼接字符串
@Value("#{testConstant.nickname + ' ' + testConstant.name}")
private String concatString;
// 對數(shù)字類型進行運算,testConstant擁有num屬性
@Value("#{ 3 * T(java.lang.Math).PI + testConstant.num}")
private double operation;
// 進行邏輯運算
@Value("#{testConstant.num > 100 and testConstant.num <= 200}")
private boolean logicOperation;
// 進行或非邏輯操作
@Value("#{ not testConstant.num == 100 or testConstant.num <= 200}")
private boolean logicOperation2;
// 使用三元運算符
@Value("#{testConstant.num > 100 ? testConstant.num : testConstant.num + 100}")
private Integer logicOperation3;
}
SpringEL使用正則表達式
public class TestSpringEL {
// 驗證是否郵箱地址正則表達式
@Value("#{testConstant.STR match '\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+'}")
private boolean regularExpression;
}
SpringEL操作集合
public class TestSpringEL {
/*
* TestConstant類中擁有名為testList的List變量, 和名為testMap的Map
*/
// 獲取下標為0的元素
@Value("#{testConstant.testList[0]}")
private String str;
// 獲取下標為0元素的大寫形式
@Value("#{testConstant.testList[0]?.toUpperCase()}")
private String upperStr;
// 獲取map中key為hello的value
@Value("#{testConstant.testMap['hello']}")
private String mapValue;
// 根據(jù)testList下標為0元素作為key獲取testMap的value
@Value("#{testConstant.testMap[testConstant.testList[0]]}")
private String mapStrByTestList;
}
Spring操作外部Properties文件
<!-- 首先通過applicaContext.xml中<util:properties>增加properties文件 --> <!-- 注意需要引入Spring的util schemea命名空間和注意id屬性,id屬性將在SpringEL中使用 --> <util:properties id="test" location="classpath:application.properties"/>
public class TestSpringEL {
// 注意test為xml文件中聲明的id
@Value("#{test['jdbc.url']}")
private String propertiesValue;
}
SpringEL查詢篩選集合和投影
public class TestSpringEL {
/*
* 聲明City類,有population屬性 testContants擁有名叫cityList的City類List集合
*/
// 過濾testConstant中cityList集合population屬性大于1000的全部數(shù)據(jù)注入到本屬性
@Value("#{testConstant.cityList.?[population > 1000]}")
private List<City> cityList;
// 過濾testConstant中cityList集合population屬性等于1000的第一條數(shù)據(jù)注入到本屬性
@Value("#{testConstant.cityList.^[population == 1000]}")
private City city;
// 過濾testConstant中cityList集合population屬性小于1000的最后一條數(shù)據(jù)注入到本屬性
@Value("#{testConstant.cityList.$[population < 1000]}")
private City city2;
/*
* 首先為city增加name屬性,代表城市的名稱
*/
/*
* 假如我們在過濾城市集合后只想保留城市的名稱,
* 可以使用如下方式進行投影
*/
@Value("#{testConstant.cityList.?[population > 1000].![name]}")
private List<String> cityName;
}
優(yōu)點:
SpringEL功能非常強大,在Annotation的方式開發(fā)時可能感覺并不強烈,因為可以直接編寫到源代碼來實現(xiàn)SpringEL的功能,但若然是在XML文件中進行配置,SpringEL可以彌補XML靜態(tài)注入的不足,從而實現(xiàn)更強大的注入
缺點:
SpringEL在使用時僅僅是一個字符串,不易于排錯與測試,也沒有IDE檢查我們的語法,當出現(xiàn)錯誤時較難檢測
筆者實際應用:
筆者開發(fā)的項目當中比較頻繁的使用SpringEL,例如通過SpringEL獲取外部properties中的值,又或者項目當中的數(shù)據(jù)字典亦是使用SpringEL的一個場景,我們抽象出一個Param類的集合,通過SpringEL集合篩選和投影獲取我們想要的字段參數(shù)添加到我們的程序邏輯當中(筆者項目中的Spring Security亦使用SpringEL,但本文章不加以敘述)
總結(jié):
Spring3.0讓人為之驚艷的非SpringEL莫屬,為我們的注入提供了另一種強大的形式,傳統(tǒng)注入能做到的事情,和做不到的事情,SpringEL一概能完成,但在項目當中并不適宜大量使用SpringEL,適當?shù)募夹g方在適當?shù)奈恢?,才能更好的完成事情?/p>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
spring cloud eureka微服務之間的調(diào)用詳解
這篇文章主要介紹了spring cloud eureka微服務之間的調(diào)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
詳解springmvc之json數(shù)據(jù)交互controller方法返回值為簡單類型

