Spring中bean的生命周期

一、说在前面

(一)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;
	}
}


配置的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 点击打开链接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章