Spring中使用SpEL为Bean的属性动态赋值


引言:

可以在Spring中使用SpEL语言为Bean的属性赋一个字面值,引用其他类的静态属性,引用其他的Bean,引用其他Bean的属性,还可以在SpEL中使用运算符。

  • Address.java
  • Car.java
  • Person.java
  • Main.java
  • beans-spel.xml

Address.java###

package com.yczlab.spring.beans.spel;

public class Address {
    private String city;
    private String street;

    public Address() {
    }

    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", street='" + street + '\'' +
                '}';
    }
}

Car.java

package com.yczlab.spring.beans.spel;

public class Car {
    private String brand;
    private double price;
    //轮胎周长
    private double tyrePerimeter;

    public Car() {
    }

    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 +
                '}';
    }
}

Person.java

package com.yczlab.spring.beans.spel;

public class Person {
    private String name;
    private Car car;
    private String city;//打算 引用address bean的city属性
    private String info;//打算 根据car的price属性确定info。price>=300000,为金领;否则为白领

    public Person() {
    }

    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 + '\'' +
                '}';
    }
}

Main.java

package com.yczlab.spring.beans.spel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

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

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

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

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

    }
}

beans-spel.xml

<?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 id="address" class="com.yczlab.spring.beans.spel.Address">
        <!--使用SpEL为属性赋一个字面值-->
        <property name="city" value="#{'BeiJing'}"/>
        <property name="street" value="WuDaoKou"/>
    </bean>

    <bean id="car" class="com.yczlab.spring.beans.spel.Car">
        <property name="brand" value="Audi"/>
        <property name="price" value="500000"/>
        <!--使用SpEL引用类的静态属性-->
        <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"/>
    </bean>

    <bean id="person" class="com.yczlab.spring.beans.spel.Person">
        <!--使用SpEL来引用其他的Bean-->
        <property name="car" value="#{car}"/>
        <!--使用SpEL来引用其他Bean的属性-->
        <property name="city" value="#{address.city}"/>
        <!-- 在SpEL中使用运算符 -->
        <property name="info" value="#{car.price > 300000 ? '金领':'白领'}"/>
        <property name="name" value="Tom"/>
    </bean>

</beans>

文章作者: YangChongZhi
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 YangChongZhi !
评论
 上一篇
Spring IOC容器对Bean生命周期的管理 Spring IOC容器对Bean生命周期的管理
引言: Spring的IOC容器对Bean生命周期的管理,包括Bean的后置处理器、初始化和销毁等 Car.java MyBeanPostProcessor.java Main.java beans-cycle.xml Car.jav
2020-03-08
下一篇 
Spring配置Bean时使用外部属性文件(比如配置数据库源) Spring配置Bean时使用外部属性文件(比如配置数据库源)
引言: Spring在配置某些Bean时可以使用外部的属性文件。使用外部化属性文件中的属性,在之后开发过程中便于修改,只需修改外部的属性文件即可。比如配置数据库时。 Main.java beans-properties.xml db.p
2020-03-08
  目录