當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Sping-Spring表达式语言SpEL
生活随笔
收集整理的這篇文章主要介紹了
Sping-Spring表达式语言SpEL
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 概述
- SpEL:字面量
- SpEL:引用 Bean、屬性和方法
- 引用其他對象
- 引用其他對象的屬性
- 調用其他方法,還可以鏈式操作
- 調用靜態方法或靜態屬性
- SpEL支持的運算符號
- 算數運算符:+, -, *, /, %, ^
- 加號還可以用作字符串連接
- 比較運算符: <, >, ==, <=, >=, lt, gt, eq, le, ge
- 邏輯運算符號: and, or, not, |
- if-else 運算符:?: (ternary), ?: (Elvis)
- if-else 的變體
- 正則表達式:matches
- 示例-基于xml的方式
- 示例-基于注解的方式
概述
Spring 表達式語言(簡稱SpEL):是一個支持運行時查詢和操作對象圖的強大的表達式語言。
語法類似于 EL:SpEL 使用 #{…} 作為定界符,所有在大框號中的字符都將被認為是 SpEL
SpEL 為 bean 的屬性進行動態賦值提供了便利.
通過 SpEL 可以實現:
通過 bean 的 id 對 bean 進行引用
調用方法以及引用對象中的屬性
計算表達式的值
正則表達式的匹配
SpEL:字面量
字面量的表示:
整數: <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表達式的,這里僅僅演示一下而已,日常的開發中很少使用。
SpEL:引用 Bean、屬性和方法
引用其他對象
但是我們更常用ref 來實現其他對象的引用
引用其他對象的屬性
調用其他方法,還可以鏈式操作
調用靜態方法或靜態屬性
通過 T() 調用一個類的靜態方法,它將返回一個 Class Object,然后再調用相應的方法或屬性:
SpEL支持的運算符號
算數運算符:+, -, *, /, %, ^
加號還可以用作字符串連接
比較運算符: <, >, ==, <=, >=, lt, gt, eq, le, ge
邏輯運算符號: and, or, not, |
if-else 運算符:?: (ternary), ?: (Elvis)
if-else 的變體
正則表達式:matches
示例-基于xml的方式
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;}@Overridepublic 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;}@Overridepublic 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的cityprivate String city;// 通過 Car的price屬性,確定info ,如果car.price>=500000 ,info 為CEO,否則為 Staffprivate 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;}@Overridepublic 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;}@Overridepublic 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.xsdhttp://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 {@Testpublic 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總結
以上是生活随笔為你收集整理的Sping-Spring表达式语言SpEL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle-AWR管理包DBMS_WO
- 下一篇: MyBatis-02 MyBatis X