Spring框架(四)—— SpringBean 的生命周期

一、SpringBean 的生命周期

1、SpringBean 生命周期概述

Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。
当我们使用注解或者bean标签,将对象的创建工作交给spring处理以后,该对象的生命周期就由spring容器来管理。

2、SpringBean 生命周期过程

Semon吹吹_Spring Bean生命周期
(1)实例化 Bean 对象

  • 调用实例化 Bean 对象之前的 InstantiationAwareBeanPostProcessorAdapter 接口的 postProcessBeforeInstantiation 方法
  • Bean 的创建,由 BeanFactory 读取 Bean 定义文件,并生成各个实例
  • 调用实例化Bean对象之后的 InstantiationAwareBeanPostProcessorAdapter 接口的 postProcessAfterInstantiation 方法

(2)设置 Bean 对象属性

  • 执行在 Bean 设置属性时的 InstantiationAwareBeanPostProcessorAdapter 接口的 postProcessPropertyValues 方法,设置pvs值
  • Setter 注入,执行 Bean 的属性依赖注入

(3)将容器和 bean 本身的信息暴露出来便于使用过程

  • 实现 BeanNameAware 接口,重写并执行 setBeanName()。
  • 实现 BeanFactoryAware接口,重写并执行 setBeanFactory()。

(4)初始化 Bean 对象过程

  • 调用初始化 Bean 对象之前的InstantiationAwareBeanPostProcessorAdapter 接口的 postProcessBeforeInitialization 方法
  • 继承 InitializingBean 类,重写 afterPropertiesSet(),完成初始化。
  • 在 xml 文件中的 Bean 标签中使用 init-method 可以设置自定义初始化方法。
  • 调用初始化 Bean 对象之后的 InstantiationAwareBeanPostProcessorAdapter 接口的 postProcessAfterInitialization 方法。

(5)销毁 Bean 对象过程

  • 实现 DisposableBean 接口,重写 destroy(),在容器关闭时,如果 Bean 类实现了该接口,则执行它的 destroy() 方法
  • 在 xml 文件中的 Bean 标签中定义 destroy-method,在容器关闭时,可以在 xml 文件中的 Bean 标签中使用“destory-method”设置自定义的对象销毁方法的方法

3、生命周期示例

(1)Student 实体类
分别实现 BeanNameAware 接口、BeanFactoryAware 接口、InitializingBean 接口、DisposableBean 接口

public class Student implements BeanNameAware,BeanFactoryAware,InitializingBean,DisposableBean{
	private String name;
	private int age;
	
	//无参构造
	public Student() {
		System.out.println("- 执行了无参构造");
	}
	//有参构造
	public Student(String name, int age) {
		System.out.println("- 执行了有参构造name=" + name + " age=" + age);
		this.name = name;
		this.age = age;
	}
	//get、set方法
	public String getName() {
		System.out.println("- 执行了getName()方法");
		return name;
	}
	public void setName(String name) {
		System.out.println("- 执行了setName()方法,调用了setter方法通过有参构造完成了属性注入,name=" + name);
		this.name = name;
	}
	public int getAge() {
		System.out.println("- 执行了getAge()方法");
		return age;
	}
	public void setAge(int age) {
		System.out.println("- 执行了setAge()方法,调用了setter方法通过有参构造完成了属性注入,age=" + age);
		this.age = age;
	}
	//已定义show()方法
	public void show() {
		System.out.println("- 执行了自定义的show()方法,name=" + name);
	}
	//自定义初始化对象方法init()
	public void init() {
		System.out.println("- 执行了自定义的初始化对象方法init()");
	}
	//自定义销毁对象方法des()
	public void des() {
		System.out.println("- 执行了自定义的销毁对象方法des()");
	}
	//实现BeanNameAware接口,重写setBeanName()方法
	@Override
	public void setBeanName(String beanName) {
		System.out.println("- 执行了BeanNameAware接口的setBeanName()方法,将bean的id暴露出来,当前对象在容器中的beanid=" + beanName);
	}
	//实现BeanFactoryAware接口,重写setBeanFactory()方法
	@Override
	public void setBeanFactory(BeanFactory factory) throws BeansException {
		System.out.println("- 执行了BeanFactoryAware接口的setBeanFactory()方法,将工厂对象暴露出来,通过该工厂可以获取容器中的对象");
		System.out.print("this==factory.getBean(“student”)的值=");
		System.out.println(this==factory.getBean("student"));
	}
	//实现InitializingBean接口,重写afterPropertiesSet()方法
	@Override
	public void afterPropertiesSet() throws Exception {
		 System.out.println("- 执行了InitializingBean接口的afterPropertiesSet()方法,初始化完成,完成了属性的注入,name = " + name);
	}
	//实现DisposableBean接口,重写destroy()方法
	@Override
	public void destroy() throws Exception {
		System.out.println("- 执行了DisposableBean接口的destroy()方法,容器被关闭,对象即将被销毁,执行Spring自带的销毁方法");
	}
}

(2)继承InstantiationAwareBeanPostProcessorAdapter类的Processor 类

//实例化之前、之后,初始化之前、之后,及框架设置Bean属性时调用该接口
public class Processor extends InstantiationAwareBeanPostProcessorAdapter{
	//实例化之前
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		System.out.println("- 执行了实例化Bean对象之前的InstantiationAwareBeanPostProcessorAdapter接口的postProcessBeforeInstantiation方法");
		return null;
	}
	//实例化之后
	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		System.out.println("- 执行了实例化Bean对象之后的InstantiationAwareBeanPostProcessorAdapter接口的postProcessAfterInstantiation方法");
		return true;
	}
	//Bean设置属性
	@Override
	public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
		System.out.println("- 执行了在Bean设置属性时的InstantiationAwareBeanPostProcessorAdapter接口的postProcessPropertyValues方法,设置pvs值=" + pvs);
		return pvs;
	}
	//初始化之前
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		Student student = (Student) bean;
		System.out.println("- 执行了初始化Bean对象之前的InstantiationAwareBeanPostProcessorAdapter接口的postProcessBeforeInitialization方法");
		student.setName("李四");
		return bean;
	}
	//初始化之后
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		Student student = (Student) bean;
		System.out.println("- 执行了初始化Bean对象之后的InstantiationAwareBeanPostProcessorAdapter接口的postProcessAfterInitialization方法");
		student.setAge(10);
		return bean;
	}
}

(3)xml 文件

<beans>
	<bean id="student" class="com.spring.demo3.entity.Student" init-method="init" destroy-method="des">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
	</bean>
	<bean class="com.spring.demo3.action.Processor"></bean>
</beans>

(5)Action 类

public class UserAction{
	public static void main(String[] args) {
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring3.xml");
		Student student = (Student) ac.getBean("student");
		student.show();
		ac.close();
	}
}

(6)得到结果

- 执行了实例化Bean对象之前的InstantiationAwareBeanPostProcessorAdapter接口的postProcessBeforeInstantiation方法
- 执行了无参构造
- 执行了实例化Bean对象之后的InstantiationAwareBeanPostProcessorAdapter接口的postProcessAfterInstantiation方法
- 执行了在Bean设置属性时的InstantiationAwareBeanPostProcessorAdapter接口的postProcessPropertyValues方法,设置pvs值=PropertyValues: length=2; bean property 'name'; bean property 'age'
- 执行了setName()方法,调用了setter方法通过有参构造完成了属性注入,name=张三
- 执行了setAge()方法,调用了setter方法通过有参构造完成了属性注入,age=20
- 执行了BeanNameAware接口的setBeanName()方法,将bean的id暴露出来,当前对象在容器中的beanid=student
- 执行了BeanFactoryAware接口的setBeanFactory()方法,将工厂对象暴露出来,通过该工厂可以获取容器中的对象
this==factory.getBean(“student”)的值=true
- 执行了初始化Bean对象之前的InstantiationAwareBeanPostProcessorAdapter接口的postProcessBeforeInitialization方法
- 执行了setName()方法,调用了setter方法通过有参构造完成了属性注入,name=李四
- 执行了InitializingBean接口的afterPropertiesSet()方法,初始化完成,完成了属性的注入,name = 李四
- 执行了自定义的初始化对象方法init()
- 执行了初始化Bean对象之后的InstantiationAwareBeanPostProcessorAdapter接口的postProcessAfterInitialization方法
- 执行了setAge()方法,调用了setter方法通过有参构造完成了属性注入,age=10
- 执行了自定义的show()方法,name=李四
- 执行了DisposableBean接口的destroy()方法,容器被关闭,对象即将被销毁,执行Spring自带的销毁方法
- 执行了自定义的销毁对象方法des()
发布了75 篇原创文章 · 获赞 10 · 访问量 2873

猜你喜欢

转载自blog.csdn.net/baidu_27414099/article/details/104440454