一、說在前面
(一)IOC 容器中 Bean 的生命週期方法
1、Spring IOC 容器可以管理 Bean 的生命週期, Spring 允許在 Bean 生命週期的特定點執行定製的任務。2、Spring IOC 容器對 Bean 的生命週期進行管理的過程:
(1)通過構造器或工廠方法創建 Bean 實例;
(2)爲 Bean 的屬性設置值和對其他 Bean 的引用;
(3)調用 Bean 的初始化方法;
(4)使用 Bean ;
(5)當容器關閉時, 調用 Bean 的銷燬方法。
3、在 Bean 的聲明裏設置 init-method 和 destroy-method 屬性, 爲 Bean 指定初始化和銷燬方法。
(二)創建 Bean 後置處理器
1、Bean 後置處理器允許在調用初始化方法前後對 Bean 進行額外的處理。2、Bean 後置處理器對 IOC 容器裏的所有 Bean 實例逐一處理, 而非單一實例。其典型應用是: 檢查 Bean 屬性的正確性或根據特定的標準更改 Bean 的屬性。對Bean 後置處理器而言, 需要實現BeanPostProcessor接口. 在初始化方法被調用前後, Spring 將把每個 Bean 實例分別傳遞給上述接口的以下兩個方法:
postProcessBeforeInitialization(Object bean, String beanName):init-method 之前調用
postProcessAfterInitialization(Object bean, String beanName):init-method 之後調用
(三)添加 Bean 後置處理器後 Bean 的生命週期
(1)通過構造器或工廠方法創建 Bean 實例;(2)爲 Bean 的屬性設置值和對其他 Bean 的引用;
(3)將 Bean 實例傳遞給 Bean 後置處理器的 postProcessBeforeInitialization 方法;
(4)調用 Bean 的初始化方法;
(5)將 Bean 實例傳遞給 Bean 後置處理器的 postProcessAfterInitialization方法;
(6)使用Bean ;
(7)當容器關閉時, 調用 Bean 的銷燬方法。
二、實例展示
Car類:
package com.at.beans.cycle;
public class Car {
public Car() {
System.out.println("Car 無參構造器");
}
private String 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 ...");
}
}
MyBeanPostProcessor類:
package com.at.beans.cycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessBeforeInitialization"+bean+","+beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization"+bean+","+beanName);
return 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.at.beans.cycle.Car"
init-method="init" destroy-method="destroy">
<property name="brand" value="Audi"></property>
</bean>
<!-- 實現BeanPostProcessor接口,並且具體提供以下兩個函數的實現
postProcessBeforeInitialization(Object bean, String beanName):init-method 之前調用
postProcessAfterInitialization(Object bean, String beanName):init-method 之後調用
bean:bean實例本身
beanName:ioc配置的bean的名字
返回值:是實際上返回給用戶的那個bean,注意:我們可以在以上的兩個方法中修改返回的bean,甚至可以返回一個新的bean
-->
<!-- 配置bean的後置處理器 :不需要配置id,ioc容器自動識別是一個BeanPostProcessor後置處理器-->
<bean class="com.at.beans.cycle.MyBeanPostProcessor"></bean>
</beans>
測試函數:
package com.at.beans.cycle;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCycle {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
//關閉ioc容器
ctx.close();
}
}
六月 01, 2017 3:15:39 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@fefe574: startup date [Thu Jun 01 15:15:39 CST 2017]; root of context hierarchy
六月 01, 2017 3:15:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-cycle.xml]
Car 無參構造器
setBrand ...
postProcessBeforeInitializationcom.at.beans.cycle.Car@4f9fe278,car
init ...
postProcessAfterInitializationcom.at.beans.cycle.Car@4f9fe278,car
com.at.beans.cycle.Car@4f9fe278
六月 01, 2017 3:15:39 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@fefe574: startup date [Thu Jun 01 15:15:39 CST 2017]; root of context hierarchy
destroy ...
By luoyepiaoxue2014
微博地址:http://weibo.com/luoyepiaoxue2014 點擊打開鏈接