日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Sping-Spring表达式语言SpEL

發(fā)布時(shí)間:2025/3/21 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sping-Spring表达式语言SpEL 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  • 概述
  • SpEL:字面量
  • SpEL:引用 Bean、屬性和方法
    • 引用其他對(duì)象
    • 引用其他對(duì)象的屬性
    • 調(diào)用其他方法,還可以鏈?zhǔn)讲僮?/li>
    • 調(diào)用靜態(tài)方法或靜態(tài)屬性
  • SpEL支持的運(yùn)算符號(hào)
    • 算數(shù)運(yùn)算符:+, -, *, /, %, ^
    • 加號(hào)還可以用作字符串連接
    • 比較運(yùn)算符: <, >, ==, <=, >=, lt, gt, eq, le, ge
    • 邏輯運(yùn)算符號(hào): and, or, not, |
    • if-else 運(yùn)算符:?: (ternary), ?: (Elvis)
    • if-else 的變體
    • 正則表達(dá)式:matches
  • 示例-基于xml的方式
    • 示例-基于注解的方式

概述

Spring 表達(dá)式語(yǔ)言(簡(jiǎn)稱SpEL):是一個(gè)支持運(yùn)行時(shí)查詢和操作對(duì)象圖的強(qiáng)大的表達(dá)式語(yǔ)言。


語(yǔ)法類似于 EL:SpEL 使用 #{…} 作為定界符,所有在大框號(hào)中的字符都將被認(rèn)為是 SpEL


SpEL 為 bean 的屬性進(jìn)行動(dòng)態(tài)賦值提供了便利.

通過 SpEL 可以實(shí)現(xiàn):

  • 通過 bean 的 id 對(duì) bean 進(jìn)行引用

  • 調(diào)用方法以及引用對(duì)象中的屬性

  • 計(jì)算表達(dá)式的值

  • 正則表達(dá)式的匹配


SpEL:字面量

字面量的表示:

整數(shù): <property name="count" value="#{5}"/>小數(shù): <property name="frequency" value="#{89.7}"/>科學(xué)計(jì)數(shù)法: <property name="capacity" value="#{1e4}"/>String可以使用單引號(hào)或者雙引號(hào)作為字符串的定界符號(hào): <property name=“name” value="#{'Chuck'}"/> 或 <property name='name' value='#{"Chuck"}'/>Boolean: <property name="enabled" value="#{false}"/>

如果僅僅是表示字面量,其實(shí)是沒有必要使用Spring EL表達(dá)式的,這里僅僅演示一下而已,日常的開發(fā)中很少使用。


SpEL:引用 Bean、屬性和方法

引用其他對(duì)象

但是我們更常用ref 來(lái)實(shí)現(xiàn)其他對(duì)象的引用


引用其他對(duì)象的屬性


調(diào)用靜態(tài)方法或靜態(tài)屬性

通過 T() 調(diào)用一個(gè)類的靜態(tài)方法,它將返回一個(gè) Class Object,然后再調(diào)用相應(yīng)的方法或?qū)傩?#xff1a;


SpEL支持的運(yùn)算符號(hào)

算數(shù)運(yùn)算符:+, -, *, /, %, ^


加號(hào)還可以用作字符串連接


比較運(yùn)算符: <, >, ==, <=, >=, lt, gt, eq, le, ge


邏輯運(yùn)算符號(hào): and, or, not, |


if-else 運(yùn)算符:?: (ternary), ?: (Elvis)


if-else 的變體


正則表達(dá)式: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;// 調(diào)用靜態(tài)方法或靜態(tài)屬性:通過 T() 調(diào)用一個(gè)類的靜態(tài)方法,它將返回一個(gè) Class Object,然后再調(diào)用相應(yīng)的方法或?qū)傩?/span>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表達(dá)式為屬性賦值一個(gè)字面值 ,當(dāng)然了,如果是字面值就沒有必要使用Spring El表達(dá)式了,這里僅僅是演示該用法 --><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>

測(cè)試類:

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);}}

結(jié)果:

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]

示例-基于注解的方式

我們通過一個(gè)數(shù)據(jù)庫(kù)的例子來(lái)演示。雖然可以通過Spring El 表達(dá)式從配置文件中加載一個(gè)參數(shù)值,比如

@Value("#{properties['jdbc.driverClassName']}")

是不是容易出錯(cuò)…. Spring提供了更好的方式 context:property-placeholder。

package com.xgj.spel.annotation;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;/*** * * @ClassName: MyDataSource* * @Description: 數(shù)據(jù)源 @Component標(biāo)注* * @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注解自動(dòng)注入屬性配置文件中對(duì)應(yīng)屬性的值* * @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());}}

運(yùn)行結(jié)果

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

總結(jié)

以上是生活随笔為你收集整理的Sping-Spring表达式语言SpEL的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。