参考代码下载github:https://github.com/changwensir/java-ee/tree/master/spring4
3).SpEL支持的运算符号
创建PersonSpEL类,其它的类自行创建
public class PersonSpEL {
private Car car;
private String name;
//根据car的price确定Info
private String info;
//引用address bean的city的属性
private String city;
//省去get,set方法
}
@Test
public void testSpEL() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/beans-spel.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address);
Car car1 = (Car) ctx.getBean("car");
System.out.println(car1);
PersonSpEL person = (PersonSpEL) ctx.getBean("person");
System.out.println(person);
}
public class CarCycle {
private String brand;
public CarCycle() {
System.out.println("CarCycle's constructor...");
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
System.out.println("setBrand...");
this.brand = brand;
}
public void init() {
System.out.println("init...");
}
public void destroy() {
System.out.println("destroy...");
}
//省去toString方法
}
@Test
public void testCycle() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/beans-cycle.xml");
CarCycle car = (CarCycle) ctx.getBean("car");
System.out.println("---->"+car);
//关闭IOC容器
ctx.close();
}
CarCycle's constructor... setBrand... init... ---->CarCycle{brand='Audi'} destroy... |
/**
* MyBeanPostProcessor
* Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理.
* Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理, 而非单一实例.
* 其典型应用是: 检查 Bean 属性的正确性或根据特定的标准更改 Bean 的属性.
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessBeforeInitialization..." + o+","+s);
//过滤
if ("car".equals(o)){
}
return o;
}
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessAfterInitialization..." + o+","+s);
CarCycle car = new CarCycle();
car.setBrand("Ford");
return car;
}
}
在beans-cycle里增加如下内容
测试方法同上,结果如下:
CarCycle's constructor... setBrand... postProcessBeforeInitialization...CarCycle{brand='Audi'},car init... postProcessAfterInitialization...CarCycle{brand='Audi'},car CarCycle's constructor... setBrand... ---->CarCycle{brand='Ford'} destroy... |