Spring SpEL 各种写法示例

项目路径

先说一下三个bean都有哪些属性

Address.java

private String city;//城市
private String street;//街道

Car.java

private String brand;//品牌
private double price;//价格
private double tyrePerimeter;//轮胎周长

Person.java

private String name;//姓名
private Car car;
private String city;//引用Address的city属性
private String info;//若car的价格>30000?金领:白领

以上bean都有对应的get/set方法和重写的toString方法

一  使用SpEL表达式写字符串

applicationContext.xml

    <bean id="address" class="com.tse.beans.Address">
        <!-- 使用SpEl表达式写字符串*意义不大 -->
        <property name="city" value="#{'北京'}"></property>
        <property name="street" value="王府井"></property>
    </bean>

Main方法中测试

    public static void main(String[] args) {
        ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        Address address = (Address) actx.getBean("address");
        System.out.println(address);
    }

执行结果

Address [city=北京, street=王府井]

二  使用SpEl表达式引用静态变量

applicationContext.xml中新增bean

<bean id="car" class="com.tse.beans.Car">
        <property name="brand" value="Audi"></property>
        <property name="price" value="200000"></property>
        <!-- 使用SpEl表达式引用静态变量 -->
        <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
    </bean>

Main中测试(基于上一条一起测试)

public static void main(String[] args) {
        ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        Address address = (Address) actx.getBean("address");
        System.out.println(address);
        Car car = (Car) actx.getBean("car");
        System.out.println(car);
    }

测试结果

Address [city=北京, street=王府井]
Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345]

三  使用SpEl表达式引用其他bean

      使用SpEl表达式引用其他bean 的属性

      使用SpEl表达式使用运算符

applicationContext.xml

<bean id="person" class="com.tse.beans.Person">
        <property name="name" value="Tom"></property>
        <!-- 使用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>
    </bean>

Main

    public static void main(String[] args) {
        ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        Address address = (Address) actx.getBean("address");
        System.out.println(address);
        Car car = (Car) actx.getBean("car");
        System.out.println(car);
        Person person = (Person) actx.getBean("person");
        System.out.println(person);
    }

执行结果

Address [city=北京, street=王府井]
Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345]
Person [name=Tom, car=Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345], city=北京, info=白领]

源码下载链接

https://download.csdn.net/download/lijian0420/10664813

转载于:https://www.cnblogs.com/gode/p/9641356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值