Spring IOC容器管理Bean的生命周期以及bean的后置处理器

Spring IOC容器管理Bean的生命周期以及bean的后置处理器

Spring IOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务。Spring IOC容器对Bean的生命周期进行管理的过程如下:

  1. 通过构造器或工厂方法创建Bean实例
  2. 为Bean的属性设置值和对其他Bean的引用
  3. 如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName()方法
  4. 如果bean实现了BeanFactoryAware接口,Spring将调用 setBeanFactory()方法,将BeanFactory容器实例传入
  5. 如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext()方法,将bean所在的应用上下文的引用传入进来。
  6. 如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization()方法
  7. 如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet()方法。类似地,如果bean使用init-method声明了初始化方法,该方法也会被调用
  8. 如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization()方法
  9. 此时,bean已经准备就绪,可以被应用程序使用了,它们将一直驻留在应用上下文中,直到该应用上下文被销毁。
  10. 如果bean实现了DisposableBean接口,Spirng将调用它的destory()接口方法。同样,如果bean使用destory-method声明销毁方法,该方法也会被调用。

在Bean的声明里设置init-method和destory-method属性,为Bean指定初始化和销毁方法。

Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理。Bean后置处理器对IOC容器里的所有Bean实例逐一处理,而非单一实例。其典型应用是:检查Bean属性的正确性或根据特定的标准更改Bean的属性。

对Bean后置处理器而言,需要实现org.springframework.beans.factory.config interface BeanPostProcessor接口,在初始化方法被调用前后,Spring将把每个Bean实例分别传递给上述接口的以下两个方法:

  • postProcessAfterInitialization(Object bean, String beanName)
  • postProcessBeforeInitialization(Object bean, String beanName)

下面代码给出一个例子,下图给出项目的文件。

Spring IOC容器管理Bean的生命周期以及bean的后置处理器_第1张图片

首先定义一个Person类,定义构造器方法,initmethod和destorymethod方法,代码如下所示

package com.spring.c1;

public class Person {
	String name;
	int age;
	public Person() {
		System.out.println("进行Person的构造器方法");
	}
	public void initmethod(){
		System.out.println("进行Person的初始化方法initmethod()");
	}
	public void destorymethod(){
		System.out.println("进行Person的销毁方法destorymethod()");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("进行Person的setName方法");
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		System.out.println("进行Person的setAge方法");
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
}

然后定义一个MyBeanPostProcessor类,实现BeanPostProcessor接口,并实现两个方法,分别为postProcessBeforeInitialization和postProcessAfterInitialization。代码如下:

package com.spring.c1;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor{
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("对"+beanName+"进行后置处理器操作(BeforeInitialization)");
		return bean;
	}
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("对"+beanName+"进行后置处理器操作(AfterInitialization)");
		return bean;
	}
}

接着在xml文件中定义bean,定义一个名为mypostprocessor的bean,IOC容器将自动检测这个后置处理器。定义一个名为person1的bean,并设置其init-method属性和destroy-method属性。至此就完成了xml文件的配置。


 
    
    

新建一个main类,用来进行测试。

package com.spring.c1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person1=(Person) aContext.getBean("person1");
        //Person person2=(Person) aContext.getBean("person2");
		System.out.println(person1);
		//System.out.println(person2);
	}
}

输出结果为,与上面所描述的“Spring IOC容器对Bean的生命周期进行管理的过程”一致:

五月 27, 2018 9:27:39 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4d405ef7: startup date [Sun May 27 21:27:39 CST 2018]; root of context hierarchy
五月 27, 2018 9:27:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
进行Person的构造器方法
进行Person的setAge方法
进行Person的setName方法
对person1进行后置处理器操作(BeforeInitialization)
进行Person的初始化方法initmethod()
对person1进行后置处理器操作(AfterInitialization)
Person [name=Tom, age=10]

你可能感兴趣的:(spring)