0
点赞
收藏
分享

微信扫一扫

SpEl


#{123} #{‘zifuchuan’} 表示数字 字符串
#{beanid} 写一个beanid 表示另一个bean的引用
#{beanid.propName}: 引用另一个bean属性
#{beanid.toString()}: 执行方法
#{T{类}。字段|方法}}: 静态方法的属性和字段

package org.ccit.com.model;

/**
* @program: Spring01
* @description
* @author: LIANG
* @create: 2021-04-13 20:44
**/
public class Customer {
//提供三个属性的get/set方法 重写toString
private String name;
private String sex;
private double pi;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public double getPi() {
return pi;
}

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

@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", pi=" + pi +
'}';
}
}

<?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,SpEL表达式


-->
<bean id="customer" class="org.ccit.com.model.Customer">
<!-- 1,#{'zifuchuan'} 表示字符串-->
<property name="name" value="#{'username'.toString()}"></property>
<!-- 2,#{123} 数字-->
<!-- <property name="pi" value="#{123}"></property>-->
<!-- 3,静态方法的属性和方法 -->
<property name="pi" value="#{T(Math).PI}"></property>


<!-- 如果sex没有事先赋值 会空指针异常 使用? 表示有值才会调用方法-->
<property name="sex" value="#{customer.sex?.toUpperCase()}"></property>


举报

相关推荐

0 条评论