Spring中的Bean配置 (13)—— Spring表达式语言:SpEl

在这里插入图片描述
Spring中的Bean配置 (1)——内容提要
Spring中的Bean配置 (2)—— IOC和DI
Spring中的Bean配置 (3)——在Spring的IOC容器里配置Bean(通过全类名(反射))——基于XML文件的方式
Spring中的Bean配置 (4)——依赖注入方式
Spring中的Bean配置 (5)——字面值
Spring中的Bean配置 (6)——引用其他Bean
Spring中的Bean配置 (7)——注入参数详解:null值和级联属性
Spring中的Bean配置 (8)—— 集合属性
Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配
Spring中的Bean配置 (10)—— 继承 Bean 配置和依赖 Bean 配置
Spring中的Bean配置 (11)——Bean的作用域
Spring中的Bean配置 (12)——使用外部属性文件
Spring中的Bean配置 (13)—— Spring表达式语言:SpEl
Spring中的Bean配置 (14)——IOC容器中Bean的生命周期
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
Spring中的Bean配置 (16)——在Spring的IOC容器里配置Bean(通过FactoryBean)——基于XML文件的方式

Spring中的Bean配置 (17)——在Spring的IOC容器里配置Bean——基于注解的方式来配置Bean

  • Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言
  • 语法类似于 ELSpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
  • SpEL bean的属性进行动态赋值提供了便利
  • 通过 SpEL 可以实现如下:

—通过 bean 的 id 对 bean 进行引用

—调用方法以及引用对象中的属性

—计算表达式的值

—正则表达式的运算

字面量的表示

–整数:< 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支持的运算符号

  • 算数运算符:+, -, *, /, %,^,:

在这里插入图片描述

  • 加号还可以用作字符串连接

在这里插入图片描述

  • 比较运算符: <, >, ==, <=, >=, lt,gt, eq, le,ge

在这里插入图片描述

  • 逻辑运算符号: and, or, not, |

在这里插入图片描述

  • if-else 运算符:?: (ternary), ?: (Elvis)

在这里插入图片描述

  • if-else的变体
    在这里插入图片描述

  • 正则表达式:matches

在这里插入图片描述
下面是代码演示

Address.java

package com.atguigu.spring.bean;

public class Address {
	
	private String city;
	private String address;
	
	public void setCity(String city) {
		
		this.city = city;
		
	}
	
	public void setAddress(String address) {
		
		this.address = address;
		
	}

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

}

Car.java

package com.atguigu.spring.bean;

public class Car {
	
	private String brand;
	private double price;
	
	public void setBrand(String brand) {
		
		this.brand = brand;
		
	}
	
	public void setPrice(Double price) {
		
		this.price = price;
		
	}
	
	public double getPrice() {
		
		return price;
		
	}

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

Person.java

package com.atguigu.spring.bean;
public class Person {
	
	private String name;
	private Car car;
	private Address address;
	private String position;

	public void setPosition(String position) {
		
		this.position = position;
		
	}

	public void setCar(Car car) {
		
		this.car = car;
		
	}
	
	public void setAddress(Address address) {
		
		this.address = address;
		
	}
	
	public void setName(String name) {
		
		this.name = name;
		
	}

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

}

application.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">

<!-- 使用SpEl引用类的静态属性 -->
<bean id="car1" class="com.atguigu.spring.bean.Car">
<property name="brand" value="aodi"></property>
<property name="price" value="#{T(java.lang.Math).PI*8000000}"></property>
</bean>

<bean id="address1" class="com.atguigu.spring.bean.Address">
<property name="city" value="fz"></property>
<property name="address" value="wudadao"></property>
</bean>

<!-- 使用SpEl来应用其他bean,同时也可以来引用其他bean的属性,也可以使用运算符 -->
<bean id="person1" class="com.atguigu.spring.bean.Person">
<property name="name" value="zlj"></property>
<property name="car" value="#{car1}"></property>
<property name="address" value="#{address1}"></property>
<property name="position" value="#{car1.getPrice()>80000 ? '白领' : '金领'}" ></property>
</bean>

</beans>

Main.java

package com.atguigu.spring.bean;

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

public class Main {

	public static void main(String[] args) {

		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
		
		Person person=(Person)applicationContext.getBean("person1");
		
		System.out.println(person);
		
	}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值