spring学习笔记(8)——SpEL

什么是SpEL

spel就是spring的表达式语言,看下图(该图来自尚硅谷)
spring学习笔记(8)——SpEL_第1张图片

SpEL很重要的一个功能就是能够实现 动态赋值

最简单的用法

spring学习笔记(8)——SpEL_第2张图片

以上的用法其实没有什么意义,并不能体现出SpEL的强大

SpEL使用

先写三个实体类

package com.zj.spel;

public class Address {

    private String province;
    private String city;

    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @Override
    public String toString() {
        return "Address [province=" + province + ", city=" + city + "]";
    }

}
package com.zj.spel;

public class Car {

    private String brand;
    private double price;
    private double tyrePerimeter;//轮胎周长

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public double getTyrePerimeter() {
        return tyrePerimeter;
    }
    public void setTyrePerimeter(double tyrePerimeter) {
        this.tyrePerimeter = tyrePerimeter;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter="
                + tyrePerimeter + "]";
    }

}
package com.zj.spel;

public class Person {

    private String name;
    private Car car;
    //引用Address的city属性
    private String city;
    //根据car的价格:价格>300000,有钱人;价格<300000:普通人
    private String info;

    public String getName() {
        return name;
    }

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

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

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

}

配置文件中使用spel


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    
    <bean id="address" class="com.zj.spel.Address">
        <property name="province" value="#{'福建'}">property>
        <property name="city" value="#{'福州'}">property>
    bean>
    
    <bean id="car" class="com.zj.spel.Car">
        <property name="brand" value="bmw">property>
        <property name="price" value="350000">property>
        
        <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}">property>
    bean>

    <bean id="person" class="com.zj.spel.Person">
        <property name="name" value="tom">property>
        
        <property name="car" value="#{car}">property>
        
        <property name="city" value="#{address.city}">property>
        
        <property name="info" value="#{car.price > 300000 ? '有钱人' : '普通人'}">property>
    bean>

beans>

写一个测试方法

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml");

        Address address = (Address) ctx.getBean("address");
        System.out.println(address);

        Car car = (Car) ctx.getBean("car");
        System.out.println(car);

        Person person = (Person) ctx.getBean("person");
        System.out.println(person);

    }

结果
这里写图片描述

用法总结

截图来自尚硅谷
spring学习笔记(8)——SpEL_第3张图片

spring学习笔记(8)——SpEL_第4张图片

spring学习笔记(8)——SpEL_第5张图片

spring学习笔记(8)——SpEL_第6张图片

你可能感兴趣的:(spring4)