spring SpEL

spring SpEL

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

概要:

spring SpEL



Spring表达式语言:SpEL

  • Spring表达式语言(简称SpEL):是一个支持执行时查询和操作对象图的强大的表达式语言
  • 语言类似于EL:SpEL使用#{…} 作为定界符,全部在大括号里的字符都将被觉得是SpEL
  • SpEL为bean的属性进行动态赋值提供了便利
  • 通过SpEL能够实现:
    • 通过bean的id对bean进行引用
    • 调用方法以及引用对象中的属性
    • 计算表达式的值
    • 正則表達式的匹配 

SpEL:字面量(仅赋予字面值,使用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}“/>

SpEL:引用Bean、属性和方法
  • 引用其它对象:
  • spring SpEL
  • 引用其它对象的属性(用普通的方式做不到的)
  • spring SpEL
  • 调用其它方法,还能够链式操作
  • spring SpEL
  • 调用静态方法或静态属性:通过T()调用一个类的静态方法,它将返回一个Class Object,然后再调用对应的方法或属性:spring SpEL


SpEL支持的运算符号
  • 算数运算符:+,-,*,/,%,^
  • spring SpEL
  • 加号还能够用作字符串连接:
  • spring SpEL
  • 比較运算符:<,>,==,<=,>=,lt,gt,eq,le,ge
  • spring SpEL
  • 逻辑运算符号:and,or,not,|
  • spring SpEL
  • if-else运算符:?:(temary),?:(Elvis)
  • spring SpEL
  • if-else的变体
  • spring SpEL
  • 正則表達式:matches
  • spring SpEL

实例代码具体解释

文件夹结构(用到的包)

spring SpEL

Address.java

package com.coslay.beans.spel;

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

	@Override
	public String toString() {
		return "Address [city=" + city + ", street=" + street + "]";
	}

}


Car.java

package com.coslay.beans.spel;

public class Car {
	private String brand;
	private double price;
	// 轮胎的周长
	private double tyrePerimeter;

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

	public Car() {
		System.out.println("Car's Constructor...");
	}

	public double getTyrePerimeter() {
		return tyrePerimeter;
	}

	public void setTyrePerimeter(double tyrePerimeter) {
		this.tyrePerimeter = tyrePerimeter;
	}

	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter="
				+ tyrePerimeter + "]";
	}

}

Person.java

package com.coslay.beans.spel;

public class Person {
	private String name;
	private Car car;
	// 引用address bean的city属性
	private String city;
	// 依据car的price确定info:car的price》=300000:金额
	// 否则为:白领
	private 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;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", car=" + car + ", city=" + city
				+ ", info=" + info + "]";
	}

}

Main.java

package com.coslay.beans.spel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"beans-spel.xml");

		Address address = (Address) ctx.getBean("address");
		System.out.println(address);
		
		Car car = (Car) ctx.getBean("car");
		System.out.println(car);
		
		Person person = (Person) ctx.getBean("person");
		System.out.println(person);
	}
}


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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	
	<bean id="address" class="com.coslay.beans.spel.Address">
		<!-- 使用spel为属性赋一个字面值 -->
		<property name="city" value="#{'beijint'}"></property>
		<property name="street" value="WuDaoKou"></property>
	</bean> 
	
	<bean id="car" class="com.coslay.beans.spel.Car">
		<property name="brand" value="Audi"></property>
		<property name="price" value="5000000"></property>
		<!-- 使用SpEL引用类的静态属性 -->
		<property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
	</bean>
	
	<bean id="person" class="com.coslay.beans.spel.Person">
		<!-- 使用 SpEL来引用其它的Bean -->
		<property name="car" value="#{car}"></property>
		<!-- 使用SpEL来引用其它的Bean的属性 -->
		<property name="city" value="#{address.city}"></property>
		<!-- SpEL中使用运算符 -->
		<property name="info" value="#{car.price > 300000 ? '金领' : '白领'}"></property>
		<property name="name" value="Tom"></property>
	</bean>
</beans>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118481.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月5日 下午4:00
下一篇 2021年12月5日 下午4:00


相关推荐

  • ArrayDeque in Java[通俗易懂]

    ArrayDeque in Java[通俗易懂]ArrayDequeinJavaArrayDequeinJavaprovidesawaytoapplyresizable-arrayinadditiontotheimplementationoftheDequeinterface.ItisalsoknownasArrayDoubleEndedQueueorArrayDeck.Thi…

    2026年2月3日
    12
  • 扣子(Coze),开源了!Dify 天塌了

    扣子(Coze),开源了!Dify 天塌了

    2026年3月12日
    2
  • PLSQL Developer连接Oracle11g 64位数据库配置详解

    PLSQL Developer连接Oracle11g 64位数据库配置详解最近换了台64bit的电脑,所以oracle数据库也跟着换成了64bit的,不过问题也随之产生,由于plsqldeveloper暂时没有64bit版本的,所以无法连接到64bit的oracle上,经过一番折腾,终于成功连接到数据库上,现记录下配置过程,以便查看。

    2022年5月26日
    58
  • linux看系统 查看数据库命令,linux查询数据库命令

    linux看系统 查看数据库命令,linux查询数据库命令在 linux 系统中有相关的命令可以让我们对 mysql 数据库做相关的操作 那么具体是哪些命令呢 下面由学习啦小编为大家整理了 linux 查询数据库命令的相关知识 希望对大家有帮助 1 linux 查看数据库的命令 mysql gt showdatabase 查看数据表的详细结构 mysql gt descfuntb 2 linux 操作数据库的相关命令新建数据库 mysql gt createda

    2026年3月17日
    1
  • shell-循环

    shell-循环接上一篇shell运算符接着往下说,shell循环:shell循环有三种,一种是for循环,一种是while循环,还有一种是until循环,循环体中和java类似,可以使用break调出当前循环,continue继续下一次循环。for循环for循环以for开始,循环体在do和done之间for循环有两种各式,一种是带in,一种是类似java的for循环:比如说输出0到10之间的整数,给…

    2022年7月24日
    13
  • 26Region_tarim logai toplam

    26Region_tarim logai toplam给出 n 个点的一棵树,多次询问两点之间的最短距离。注意:边是无向的。所有节点的编号是 1,2,…,n。输入格式第一行为两个整数 n 和 m。n 表示点数,m 表示询问次数;下来 n−1 行,每行三个整数 x,y,k,表示点 x 和点 y 之间存在一条边长度为 k;再接下来 m 行,每行两个整数 x,y,表示询问点 x 到点 y 的最短距离。树中结点编号从 1 到 n。输出格式共 m 行,对于每次询问,输出一行询问结果。数据范围2≤n≤104,1≤m≤2×104,0<k≤1

    2022年8月10日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号