国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
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注入簡單值

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * @Value注解等同于XML配置中的<property/>標簽,  
  5.      * SpringEL同樣支持在XML<property/>中編寫 
  6.      */  
  7.   
  8.     // 注入簡單值,輸出num為5  
  9.     @Value("#{5}")  
  10.     private Integer num;  
  11.   
  12.     // 注入ID為testConstant的Bean  
  13.     @Value("#{testConstant}")  
  14.     private TestConstant Constant;  
  15.   
  16.     // 注入ID為testConstant Bean中的STR常量/變量  
  17.     @Value("#{testConstant.STR}")  
  18.     private String str;  
  19.   
  20. }  


      使用SpringEL調(diào)用方法

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * TestConstant類中有兩個方法重載, 
  5.      * 返回值為String類型 
  6.      */  
  7.   
  8.     // 調(diào)用無參方法  
  9.     @Value("#{testConstant.showProperty}")  
  10.     private String method1;  
  11.   
  12.       
  13.     // 有參接收字符串的方法  
  14.     @Value("#{testConstant.showProperty('Hello')}")  
  15.     private String method2;  
  16.   
  17.       
  18.     /* 
  19.      * 若然希望方法返回的String為大寫 
  20.      */  
  21.     @Value("#{testConstant.showProperty().toUpperCase()}")  
  22.     private String method3;  
  23.   
  24.       
  25.     /* 
  26.      * 若使用method3這種方式,若然showProperty返回為null,  
  27.      * 將會拋出NullPointerException,可以使用以下方式避免 
  28.      */  
  29.     @Value("#{testConstant.showProperty()?.toUpperCase}")  
  30.     private String method4;  
  31.     /* 
  32.      * 使用?.符號代表若然左邊的值為null,將不執(zhí)行右邊方法,  
  33.      * 讀者可以靈活運用在其他場景,只要左邊可能返回null, 
  34.      * 即可使用上面示例中的?. 
  35.      */  
  36. }  


      SpringEL調(diào)用靜態(tài)類或常量

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 注入JDK中的工具類常量或調(diào)用工具類的方法 
  5.      */  
  6.   
  7.     // 獲取Math的PI常量  
  8.     @Value("#{T(java.lang.Math).PI")  
  9.     private double pi;  
  10.   
  11.     // 調(diào)用random方法獲取返回值  
  12.     @Value("#{T(java.lang.Math).random()}")  
  13.     private double ramdom;  
  14.   
  15.     // 獲取文件路徑符號  
  16.     @Value("#{T(java.io.File).separator}")  
  17.     private String separator;  
  18. }  


      SpringEL運算

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 使用SpringEL進行運算及邏輯操作 
  5.      */  
  6.   
  7.     // 拼接字符串  
  8.     @Value("#{testConstant.nickname + ' ' + testConstant.name}")  
  9.     private String concatString;  
  10.   
  11.     // 對數(shù)字類型進行運算,testConstant擁有num屬性  
  12.     @Value("#{ 3 * T(java.lang.Math).PI + testConstant.num}")  
  13.     private double operation;  
  14.   
  15.     // 進行邏輯運算  
  16.     @Value("#{testConstant.num > 100 and testConstant.num <= 200}")  
  17.     private boolean logicOperation;  
  18.   
  19.     // 進行或非邏輯操作  
  20.     @Value("#{ not testConstant.num == 100 or testConstant.num <= 200}")  
  21.     private boolean logicOperation2;  
  22.   
  23.     // 使用三元運算符  
  24.     @Value("#{testConstant.num > 100 ? testConstant.num : testConstant.num + 100}")  
  25.     private Integer logicOperation3;  
  26. }  


      SpringEL使用正則表達式

Java代碼  
  1. public class TestSpringEL {  
  2.     // 驗證是否郵箱地址正則表達式  
  3.     @Value("#{testConstant.STR match '\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+'}")  
  4.     private boolean regularExpression;  
  5. }  


      SpringEL操作集合

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * TestConstant類中擁有名為testList的List變量, 和名為testMap的Map 
  5.      */  
  6.   
  7.     // 獲取下標為0的元素  
  8.     @Value("#{testConstant.testList[0]}")  
  9.     private String str;  
  10.   
  11.     // 獲取下標為0元素的大寫形式  
  12.     @Value("#{testConstant.testList[0]?.toUpperCase()}")  
  13.     private String upperStr;  
  14.   
  15.     // 獲取map中key為hello的value  
  16.     @Value("#{testConstant.testMap['hello']}")  
  17.     private String mapValue;  
  18.   
  19.     // 根據(jù)testList下標為0元素作為key獲取testMap的value  
  20.     @Value("#{testConstant.testMap[testConstant.testList[0]]}")  
  21.     private String mapStrByTestList;  
  22. }  


      Spring操作外部Properties文件


      首先通過applicaContext.xml中<util:properties>增加properties文件

Xml代碼  
  1. <!-- 注意需要引入Spring的util schemea命名空間和注意id屬性,id屬性將在SpringEL中使用 -->  
  2.     <util:properties id="test" location="classpath:application.properties" />  

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     // 注意test為xml文件中聲明的id  
  4.     @Value("#{test['jdbc.url']}")  
  5.     private String propertiesValue;  
  6. }  


      SpringEL查詢篩選集合和投影

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 聲明City類,有population屬性 testContants擁有名叫cityList的City類List集合 
  5.      */  
  6.   
  7.     // 過濾testConstant中cityList集合population屬性大于1000的全部數(shù)據(jù)注入到本屬性  
  8.     @Value("#{testConstant.cityList.?[population > 1000]}")  
  9.     private List<City> cityList;  
  10.   
  11.     // 過濾testConstant中cityList集合population屬性等于1000的第一條數(shù)據(jù)注入到本屬性  
  12.     @Value("#{testConstant.cityList.^[population == 1000]}")  
  13.     private City city;  
  14.   
  15.     // 過濾testConstant中cityList集合population屬性小于1000的最后一條數(shù)據(jù)注入到本屬性  
  16.     @Value("#{testConstant.cityList.$[population < 1000]}")  
  17.     private City city2;  
  18.   
  19.     /* 
  20.      * 首先為city增加name屬性,代表城市的名稱 
  21.      */  
  22.       
  23.     /* 
  24.      * 假如我們在過濾城市集合后只想保留城市的名稱, 
  25.      * 可以使用如下方式進行投影 
  26.      */  
  27.     @Value("#{testConstant.cityList.?[population > 1000].![name]}")  
  28.     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ù)奈恢?才能更好的完成事情 
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
String 字符串對象
Spring為屬性注入某個類的常量或方法的返回值
XMemcached與Spring3.2緩存框架集成
spring-boot(五)自定義屬性配置
spring的Bean的基礎配置2
spring通過構(gòu)造器的方式bean注入(方法二)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服