Spring SpEL(Spring_表达式语言)

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a458383896/article/details/84957264

       对<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>

运行结果:

猜你喜欢

转载自blog.csdn.net/a458383896/article/details/84957264