Spring值SpEL

public class Car {
    
    private String brand;
    private double price;
    private double typePrimeter;

    public double getTypePrimeter() {
        return typePrimeter;
    }

    public void setTypePrimeter(double typePrimeter) {
        this.typePrimeter = typePrimeter;
    }

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

    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + ", typePrimeter="
                + typePrimeter + "]";
    }
}
public class Person {
    
    private String name;
    private Car car;
    private Address address;
    private String info;
    
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = 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 Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", car=" + car + ", address=" + address
                + ", info=" + info + "]";
    }
}
<?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="car" class="com.auguigu.spring.beans.spel.Car">
        <property name="brand" value="Audi"></property>
        <property name="price" value="30000"></property>
        <property name="typePrimeter" value="#{T(java.lang.Math).PI*80}"></property>
    </bean>    
    <bean id="person" class="com.auguigu.spring.beans.spel.Person">
        <property name="name" value="zhangsan"></property>
        <property name="car" value="#{car}"></property>
        <property name="info" value="#{car.price>10000?'白领':'金领'}"></property>
    </bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("./beans-spel.xml");
        Person p = (Person) ctx.getBean("person");
        System.out.println(p);
        
    }

}
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Person [name=zhangsan, car=Car [brand=Audi, price=30000.0, typePrimeter=251.32741228718345], address=null, info=白领]
原文地址:https://www.cnblogs.com/sdnu-zhang/p/8527973.html