Spring阶段性学习总结(四)SPEL

 1 public class Address {
 2     private String city;
 3     private String street;
 4 
 5     public String getCity() {
 6         return city;
 7     }
 8 
 9     public void setCity(String city) {
10         this.city = city;
11     }
12 
13     public String getStreet() {
14         return street;
15     }
16 
17     public void setStreet(String street) {
18         this.street = street;
19     }
20 
21     @Override
22     public String toString() {
23         return "Address{" +
24                 "city='" + city + '\'' +
25                 ", street='" + street + '\'' +
26                 '}';
27     }
28 }
package SpringSPEL;

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

    public double getTyrePerimeter() {
        return tyrePerimeter;
    }

    public void setTyrePerimeter(double tyrePerimeter) {
        this.tyrePerimeter = 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;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", tyrePerimeter=" + tyrePerimeter +
                '}';
    }
}
 1 package SpringSPEL;
 2 
 3 public class Person {
 4     private String name;
 5     private Car car;
 6     private String city;
 7     //car 的price>300000是金领,否则是白领
 8     private String info;
 9 
10     public String getCity() {
11         return city;
12     }
13 
14     @Override
15     public String toString() {
16         return "Person{" +
17                 "name='" + name + '\'' +
18                 ", car=" + car +
19                 ", city='" + city + '\'' +
20                 ", info='" + info + '\'' +
21                 '}';
22     }
23 
24     public void setCity(String city) {
25         this.city = city;
26     }
27 
28     public String getInfo() {
29         return info;
30     }
31 
32     public void setInfo(String info) {
33         this.info = info;
34     }
35 
36     public String getName() {
37         return name;
38     }
39 
40     public void setName(String name) {
41         this.name = name;
42     }
43 
44     public Car getCar() {
45         return car;
46     }
47 
48     public void setCar(Car car) {
49         this.car = car;
50     }
51 }
 1 package SpringSPEL;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("SpringSPEL/beans-spel.xml");
 8         Address adress = (Address) classPathXmlApplicationContext.getBean("adress");
 9         System.out.println(adress);
10         Car car = (Car) classPathXmlApplicationContext.getBean("car");
11         System.out.println(car);
12         Person person = (Person) classPathXmlApplicationContext.getBean("person");
13         System.out.println(person);
14     }
15 }

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="adress" class="SpringSPEL.Address">
 7         <property name="city" value="#{'Beijing'}"></property>
 8         <property name="street" value="#{'王府井'}"></property>
 9     </bean>
10 
11     <bean id="car" class="SpringSPEL.Car">
12         <property name="brand" value="BaoMa"></property>
13         <property name="price" value="50000"></property>
14         <!--使用spel引用类的静态常量属性-->
15         <property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
16     </bean>
17 
18     <bean id="person" class="SpringSPEL.Person">
19         <property name="car" value="#{car}"></property>
20         <property name="city" value="#{adress.city}"></property>
21         <property name="info" value="#{car.price>30000?'金领':'白领'}"></property>
22         <property name="name" value="#{'张浩'}"></property>
23     </bean>
24 </beans>

 

posted @ 2019-09-20 14:52  我叫张小凡  阅读(176)  评论(0编辑  收藏  举报