1.SpEL为bean动态赋值
2.Address.java
package com.huangliusong.spring.autowire.spel;public class Address { private String city; public String getCity() { return city; } @Override public String toString() { return "Address [city=" + city + ", street=" + street + "]"; } public Address() { super(); // TODO Auto-generated constructor stub } public void setCity(String city) { this.city = city; } public Address(String city, String street) { super(); this.city = city; this.street = street; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } private String street;}3.Car.java
package com.huangliusong.spring.autowire.spel;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 Car(String brand, double price) { super(); this.brand = brand; this.price = price; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]"; } public void setBrand(String brand) { this.brand = brand; } public Car() { System.err.println("car 构造方法"); } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }4.Person.java
package com.huangliusong.spring.autowire.spel;public class Person { private String name; private String info; // 引用address的city属性 private String city; public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } // 根据car的price确定info car的price>=300000; 金领 // 否则为白领 public String getName() { return name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public void setName(String name) { this.name = name; } public Person() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Person [name=" + name + ", info=" + info + ", city=" + city + ", car=" + car + "]"; } public Person(String name, String info, String city, Car car) { super(); this.name = name; this.info = info; this.city = city; this.car = car; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } private Car car;}5.TestAutowire.java
package com.huangliusong.spring.autowire.spel;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAutowire { @Test public void test1() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "bean_spel.xml"); Address address = (Address) ctx.getBean("address"); System.out.println(address); } @Test public void test2() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "bean_spel.xml"); Car car = (Car) ctx.getBean("car"); System.out.println(car); } @Test public void test3() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "bean_spel.xml"); Person car = (Person) ctx.getBean("person"); System.out.println(car); }}
6.bean_spel.xml