Spring IOC容器可以管理Bean的生命周期,也允许在Bean生命周期的特定点执行定制的任务。
Spring IOC容器对Bean的生命周期进行管理的过程如下:
在Bean的声明里设置init-method和destory-method属性,为Bean指定初始化和销毁方法。
测试,创建一个Car类:
package com.stuspring.cycle;
/** * Created by bee on 17/4/26. */
public class Car {
public Car() {
System.out.println("Constructor....");
}
private String brand;
public void setBrand(String brand) {
System.out.println("set Brand...");
this.brand = brand;
}
public void initMethod(){
System.out.println("init method....");
}
public void destoryMethod(){
System.out.println("destory method...");
}
}
创建beans-cycle.xml并配置一个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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.stuspring.cycle.Car" p:brand="Audi" init-method="initMethod" destroy-method="destoryMethod"/>
</bean
测试:
package com.stuspring.cycle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** * Created by bee on 17/4/26. */
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
((ConfigurableApplicationContext) ctx).close();
}
}
运行结果:
Constructor.... set Brand... init method.... com.stuspring.cycle.Car@604ed9f0 destory method...
Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理,Bean后置处理器会对IOC容器中的所有Bean实例逐一处理,要想针对特定Bean进行处理可以根据beanName参数进行判断。Bean后置处理器的典型应用是检查Bean属性的正确性或者根据特定的标准更改Bean的属性。
使用Bean后置处理器需要实现BeanPostProcessor接口,在Bean初始化方法被调用前后,Spring将把每个Bean实例分别传递给Bean接口的Object postProcessBeforeInitialization(Object bean, String beanName)
和Object postProcessAfterInitialization(Object bean, String beanName)
方法。
下面是测试例子:
新建一个Car类:
package com.stuspring.cycle;
/** * Created by bee on 17/4/26. */
public class Car {
private String brand;
public Car() {
System.out.println("Constructor....");
}
public void setBrand(String brand) {
System.out.println("set Brand...");
this.brand = brand;
}
public void initMethod() {
System.out.println("init method....");
}
public void destoryMethod() {
System.out.println("destory method...");
}
public String getBrand() {
return brand;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
'}';
}
}
创建BeanPostProcessor的实现类:
package com.stuspring.cycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/** * Created by bee on 17/4/27. */
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization: " + bean + "," + beanName);
if ("car".equals(beanName)){
Car car=new Car();
car.setBrand("BaoMa");
return car;
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization: " + bean + "," + beanName);
return bean;
}
}
创建Bean:
<bean id="car" class="com.stuspring.cycle.Car" p:brand="Audi" init-method="initMethod"
destroy-method="destoryMethod"/>
<bean class="com.stuspring.cycle.MyBeanPostProcessor"/>
Main函数中测试:
package com.stuspring.cycle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** * Created by bee on 17/4/26. */
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car.toString());
System.out.println(car.getBrand());
((ConfigurableApplicationContext) ctx).close();
}
}
运行结果:
Constructor.... set Brand... postProcessBeforeInitialization: Car{brand='Audi'},car
Constructor.... set Brand... init method.... postProcessAfterInitialization: Car{brand='BaoMa'},car
Car{brand='BaoMa'}
BaoMa
destory method...
添加Bean的后置处理器以后Bean的生命周期如下;