Spring SpEL(Spring_表达式语言)

本文深入探讨Spring SpEL表达式的应用,展示如何利用#{...}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       对<property>进行统一编程,所有的内容都使用value

       <property name="" value="#{表达式}">

       #{123}、#{'jack'} : 数字、字符串

       #{beanId}      :另一个bean引用

       #{beanId.propName}     :操作数据

       #{beanId.toString()}     :执行方法

       #{T(类).字段|方法}      :静态方法或字段

      建议读


public class Customer {
	private String cname = "jake";
	private Double pi;//= Math.PI;

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public Double getPi() {
		return pi;
	}

	public void setPi(Double pi) {
		this.pi = pi;
	}

	@Override
	public String toString() {
		return "Customer [cname=" + cname + ", pi=" + pi + "]";
	}
	
}
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpEL {
	@Test
	public void demo02 () {
		String xmlPath = "com/ithema/f_xml/d_spel/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		Customer customer = (Customer) applicationContext.getBean("customerId");
		System.out.println(customer);
	}
}
<?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-4.3.xsd">
        
     <!-- <property name="cname" value="#{'jake'}"></property> 
     	  <property name="cname" value="#{customerId.name?.toUpperCase()}"></property>
     		通过另一个bean,获得属性,调用的方法
		<property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
			?.  如果对象不为null,将调用方法
     -->
	<bean id="customerId" class="com.ithema.f_xml.d_spel.Customer">
		<property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
		<property name="pi" value="#{T(java.lang.Math).PI}"></property>
	</bean>
</beans>

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dayaoK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值