欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
SpringEL詳解及應用
什么是SpringEL?
      Spring3中引入了Spring表達式語(yǔ)言—SpringEL,SpEL是一種強大,簡(jiǎn)潔的裝配Bean的方式,他可以通過(guò)運行期間執行的表達式將值裝配到我們的屬性或構造函數當中,更可以調用JDK中提供的靜態(tài)常量,獲取外部Properties文件中的的配置


為什么要使用SpringEL?
      我們平常通過(guò)配置文件或Annotaton注入的Bean,其實(shí)都可以稱(chēng)為靜態(tài)性注入,試想一下,若然我Bean A中有變量A,它的值需要根據Bean B的B變量為參考,在這場(chǎng)景下靜態(tài)注入就對這樣的處理顯得非常無(wú)力,而Spring3增加的SpringEL就可以完全滿(mǎn)足這種需求,而且還可以對不同Bean的字段進(jìn)行計算再進(jìn)行賦值,功能非常強大


如何使用SpringEL?
      SpringEL從名字來(lái)看就能看出,和EL是有點(diǎn)關(guān)系的,SpringEL的使用和EL表達式的使用非常相似,EL表達式在JSP頁(yè)面更方便的獲取后臺中的值,而SpringEL就是為了更方便獲取Spring容器中的Bean的值,EL使用${},而SpringEL使用#{}進(jìn)行表達式的聲明

      使用SpringEL注入簡(jiǎn)單值

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * @Value注解等同于XML配置中的<property/>標簽,  
  5.      * SpringEL同樣支持在XML<property/>中編寫(xiě) 
  6.      */  
  7.   
  8.     // 注入簡(jiǎn)單值,輸出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調用方法

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * TestConstant類(lèi)中有兩個(gè)方法重載, 
  5.      * 返回值為String類(lèi)型 
  6.      */  
  7.   
  8.     // 調用無(wú)參方法  
  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為大寫(xiě) 
  20.      */  
  21.     @Value("#{testConstant.showProperty().toUpperCase()}")  
  22.     private String method3;  
  23.   
  24.       
  25.     /* 
  26.      * 若使用method3這種方式,若然showProperty返回為null,  
  27.      * 將會(huì )拋出NullPointerException,可以使用以下方式避免 
  28.      */  
  29.     @Value("#{testConstant.showProperty()?.toUpperCase}")  
  30.     private String method4;  
  31.     /* 
  32.      * 使用?.符號代表若然左邊的值為null,將不執行右邊方法,  
  33.      * 讀者可以靈活運用在其他場(chǎng)景,只要左邊可能返回null, 
  34.      * 即可使用上面示例中的?. 
  35.      */  
  36. }  


      SpringEL調用靜態(tài)類(lèi)或常量

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 注入JDK中的工具類(lèi)常量或調用工具類(lèi)的方法 
  5.      */  
  6.   
  7.     // 獲取Math的PI常量  
  8.     @Value("#{T(java.lang.Math).PI")  
  9.     private double pi;  
  10.   
  11.     // 調用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進(jìn)行運算及邏輯操作 
  5.      */  
  6.   
  7.     // 拼接字符串  
  8.     @Value("#{testConstant.nickname + ' ' + testConstant.name}")  
  9.     private String concatString;  
  10.   
  11.     // 對數字類(lèi)型進(jìn)行運算,testConstant擁有num屬性  
  12.     @Value("#{ 3 * T(java.lang.Math).PI + testConstant.num}")  
  13.     private double operation;  
  14.   
  15.     // 進(jìn)行邏輯運算  
  16.     @Value("#{testConstant.num > 100 and testConstant.num <= 200}")  
  17.     private boolean logicOperation;  
  18.   
  19.     // 進(jìn)行或非邏輯操作  
  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類(lèi)中擁有名為testList的List變量, 和名為testMap的Map 
  5.      */  
  6.   
  7.     // 獲取下標為0的元素  
  8.     @Value("#{testConstant.testList[0]}")  
  9.     private String str;  
  10.   
  11.     // 獲取下標為0元素的大寫(xiě)形式  
  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.     // 根據testList下標為0元素作為key獲取testMap的value  
  20.     @Value("#{testConstant.testMap[testConstant.testList[0]]}")  
  21.     private String mapStrByTestList;  
  22. }  


      Spring操作外部Properties文件


      首先通過(guò)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查詢(xún)篩選集合和投影

Java代碼  
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 聲明City類(lèi),有population屬性 testContants擁有名叫cityList的City類(lèi)List集合 
  5.      */  
  6.   
  7.     // 過(guò)濾testConstant中cityList集合population屬性大于1000的全部數據注入到本屬性  
  8.     @Value("#{testConstant.cityList.?[population > 1000]}")  
  9.     private List<City> cityList;  
  10.   
  11.     // 過(guò)濾testConstant中cityList集合population屬性等于1000的第一條數據注入到本屬性  
  12.     @Value("#{testConstant.cityList.^[population == 1000]}")  
  13.     private City city;  
  14.   
  15.     // 過(guò)濾testConstant中cityList集合population屬性小于1000的最后一條數據注入到本屬性  
  16.     @Value("#{testConstant.cityList.$[population < 1000]}")  
  17.     private City city2;  
  18.   
  19.     /* 
  20.      * 首先為city增加name屬性,代表城市的名稱(chēng) 
  21.      */  
  22.       
  23.     /* 
  24.      * 假如我們在過(guò)濾城市集合后只想保留城市的名稱(chēng), 
  25.      * 可以使用如下方式進(jìn)行投影 
  26.      */  
  27.     @Value("#{testConstant.cityList.?[population > 1000].![name]}")  
  28.     private List<String> cityName;  


      優(yōu)點(diǎn):SpringEL功能非常強大,在A(yíng)nnotation的方式開(kāi)發(fā)時(shí)可能感覺(jué)并不強烈,因為可以直接編寫(xiě)到源代碼來(lái)實(shí)現SpringEL的功能,但若然是在XML文件中進(jìn)行配置,SpringEL可以彌補XML靜態(tài)注入的不足,從而實(shí)現更強大的注入

      缺點(diǎn):SpringEL在使用時(shí)僅僅是一個(gè)字符串,不易于排錯與測試,也沒(méi)有IDE檢查我們的語(yǔ)法,當出現錯誤時(shí)較難檢測


      筆者實(shí)際應用:筆者開(kāi)發(fā)的項目當中比較頻繁的使用SpringEL,例如通過(guò)SpringEL獲取外部properties中的值,又或者項目當中的數據字典亦是使用SpringEL的一個(gè)場(chǎng)景,我們抽象出一個(gè)Param類(lèi)的集合,通過(guò)SpringEL集合篩選和投影獲取我們想要的字段參數添加到我們的程序邏輯當中(筆者項目中的Spring Security亦使用SpringEL,但本文章不加以敘述)


      總結:Spring3.0讓人為之驚艷的非SpringEL莫屬,為我們的注入提供了另一種強大的形式,傳統注入能做到的事情,和做不到的事情,SpringEL一概能完成,但在項目當中并不適宜大量使用SpringEL,適當的技術(shù)方在適當的位置,才能更好的完成事情 
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
String 字符串對象
Spring為屬性注入某個(gè)類(lèi)的常量或方法的返回值
XMemcached與Spring3.2緩存框架集成
spring-boot(五)自定義屬性配置
spring的Bean的基礎配置2
spring通過(guò)構造器的方式bean注入(方法二)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久