Spring表達式語言SpEL用法詳解
這篇文章主要介紹了spring表達式語言SpEL用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
(1)spring表達式語言是一個支持運行時查詢和操作對象圖得我強大表達式語言。
(2)語言類似于EL:SpEL使用#{...}作為定界符。所有在大括號中的字符串均被認為是SpEL。
(3)SpEL為bean的屬性進行動態(tài)賦值提供了便利。
(4)通過SpEL可以實現(xiàn):
- 通過Bean的id對Bean進行引用
- 調(diào)用方法及引用對象的屬性
- 計算表達式的值
- 正則表達式匹配
字面量的表示:
- 整型:#{5}
- 小數(shù):#{3.45}
- 科學(xué)計數(shù)法:#[1e4]
- 字符串:可以使用單引號或雙引號早味字符串的定界符:#{'tom'},#{"tom"}
- 布爾值:#{false}
SpEL支持的運算符號:+ - * / % ^ < > == <= >= lt gt eq le ge and or not | (?:) if-else 正則表達式等等
調(diào)用靜態(tài)屬性和方法:通過T()
以下代碼均忽略類中的getter、setter和to_string方法。
Car.java
package com.gong.spring.beans.spel; public class Car { public Car() { } public Car(String name) { this.name = name; } private String name; //輪胎周長 private double tyrePerimeter; private double price;}
Address.java
package com.gong.spring.beans.spel; public class Address { private String city; private String street; }
Student.java
package com.gong.spring.beans.spel; public class Student { private String name; private int age; private double score; private String city; private String info; private Car car; private Address address; }
beans-spel.xml
<?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.gong.spring.beans.spel.Car" p:name="baoma" p:tyrePerimeter="#{T(java.lang.Math).PI*80}"></bean> <bean id="address" class="com.gong.spring.beans.spel.Address" p:city="武漢" p:street="#{'絡(luò)南街道'}"></bean> <bean id="student" class="com.gong.spring.beans.spel.Student" p:name="tom" p:age="#{12}" p:score="#{99.00}"> <!-- 使用spel引用其它的bean --> <property name="car" value="#{car}"></property> <!-- 使用spel引用其它bean的屬性 --> <property name="city" value="#{address.city}"></property> <!-- 使用spel運算符 ,進行動態(tài)賦值--> <property name="info" value="#{car.price >300000 ? '有錢' : '沒錢'}"></property> </bean> </beans>
Main.java
package com.gong.spring.beans.spel; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.創(chuàng)建spring的IOC容器對象 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml"); //2.從容器中獲取Bean實例 Student student = (Student) ctx.getBean("student"); System.out.println(student.toString()); } }
部分輸出:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺為例)
欄 目:Java
本文標(biāo)題:Spring表達式語言SpEL用法詳解
本文地址:http://mengdiqiu.com.cn/a1/Java/8730.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時隊列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10SPRING BOOT啟動命令參數(shù)及源碼詳析
- 01-10springboot集成fastDfs過程代碼實例
- 01-10springmvc級聯(lián)屬性處理無法轉(zhuǎn)換異常問題解決
- 01-10SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解
- 01-10Spring注解和同步鎖不能同步問題解決


閱讀排行
本欄相關(guān)
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10利用Java實現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實例
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10基于Java驗證jwt token代碼實例
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery