spring 使用spel

1.car类

package com.hxzy;


public class Car {


private String brand;
private String address;
private int speed;
private double perimeter;

public void setBrand(String brand) {
this.brand = brand;
}

public void setAddress(String address) {
this.address = address;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}

public double getPerimeter() {
return perimeter;
}

public Car() {

}

public Car(String brand, String address, int speed) {
super();
this.brand = brand;
this.address = address;
this.speed = speed;
}


@Override
public String toString() {
return "Car [brand=" + brand + ", address=" + address + ", speed="
+ speed + ", perimeter=" + perimeter + "]";
}


}

2.person类

package com.hxzy;


public class Person {


private String name;
private Car car;

private String city;
//根据car的xx确定info
private String info;

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

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

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

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


public Person(String name, Car car, String city, String info) {
super();
this.name = name;
this.car = car;
this.city = city;
this.info = info;
}


public Person() {
}


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


}

3.address类

package com.hxzy;


public class Address {


private String city;
private String street;


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 + "]";
}


public Address(String city, String street) {
super();
this.city = city;
this.street = street;
}


public Address() {
super();
}


}


4.bean配置类

<?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.hxzy.Car">
        <!-- 使用spel赋值字符串 -->
        <property name="brand" value="#{'雪佛兰'}"></property>
        <property name="address" value="上汽"></property>
        <property name="speed" value="#{'210'}"></property>
        <!-- 使用spel引用类的静态属性 -->
        <property name="perimeter" value="#{T(java.lang.Math).PI * 80}"></property>
    </bean>
    
   <bean id="address" class="com.hxzy.Address">
       <property name="city" value="#{'郑州'}"></property>
       <property name="street" value="#{'南阳路'}"></property>
   </bean>
    
    <bean id="person" class="com.hxzy.Person">
        <property name="name" value="快乐的人"></property>
        <property name="car" value="#{car}"></property>
        <!-- 使用spel引用其他bean的属性 #{car.address}用法错误,不知为何报错? -->
        <!--property name="city" value="#{car.address}"></property-->
        <property name="city" value="#{address.city}"></property>
        <!--  property name="city" value="合肥"></property-->
        <!-- spel使用运算符 -->

<!--使用如下方式时提示错误Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 4): Field or property 'speed' cannot be found on object of type 'com.hxzy.Car' ,不知为何?-->
        <!--  property name="info" value="#{car.speed > 200 ? '好车' : '普通车'}"></property-->

<!--使用如下方式时正确-->
         <property name="info" value="#{address.street.equals('南阳路') ? '合资车' : '垃圾车'}"></property>
        <!--property name="info" value="好车"></property-->
    </bean>


</beans>

5.测试类

AbstractApplicationContext  ctx = new ClassPathXmlApplicationContext("spel.xml");

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值