[Spring实战系列](10)初探Bean生命周期

1. 生命周期流程图

QQ截图20160204152653.png
Bean在Spring容器中从创建到销毁经历了若干个阶段,在每一个阶段都可以针对Spring如何管理Bean进行个性化定制。

正如你所见,在Bean 准备就绪之前,Bean 工厂执行了若干启动步骤。

  • Spring 对Bean 进行实例化
  • Spring 将值和Bean 的引用注入进Bean 对应的属性中。
  • 如果Bean 实现了BeanNameAware接口,Spring 将Bean的ID传递给setBeanName() 接口方法
  • 如果Bean 实现了BeanFactoryAware接口,Spring 将调用setBeanFactory()接口方法,将BeanFactory 容器实例传入。
  • 如果Bean 实现了ApplicationContextAware 接口,Spring 将调用setApplicationContext()接口方法,将应用上下文的引用传入。
  • 如果Bean 实现了BeanPostProcessor 接口,Spring 将调用它们的postProcessBeforeInitialization() 接口方法
  • 如果Bean 实现了InitializingBean 接口,Spring 将调用它们的afterPropertiesSet() 接口方法。类似地,如果Bean 使用init-method 声明了初始化方法,该方法也会被调用。
  • 如果Bean 实现了BeanPostProcessor 接口,Spring 将调用它们的postPoressAfterInitialization() 接口方法
  • 此时此刻,Bean 已经准备就绪,可以被应用程序使用了,它们将一直驻留在应用上下文中,直到该应用上下文被销毁。
  • 如果Bean 实现了DisposableBean 接口,Spring 将调用它的destroy()接口方法。同样,如果Bean 使用destroy-method 声明了销毁方法,该方法也会被调用。

2. 各种接口方法分类
 
Bean的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:
方法 用途
Bean自身的方法 这个包括了Bean本身调用的方法和通过配置文件中<bean>的init-method和destroy-method指定的方法
Bean级生命周期接口方法 这个包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法
容器级生命周期接口方法 这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。
工厂后处理器接口方法 这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。
   
3. 实例

我们用一个简单的Spring Bean来演示一下Spring Bean的生命周期。
 
首先是一个简单的Spring Bean,它实现了BeanNameAware,BeanFactoryAware,InitializingBean,ApplicationContextAware和DiposableBean这5个接口,同时有2个方法(myInit()方法和myDestroy方法),对应配置文件中<bean>的init-method和destroy-method。

   
   
package com.sjf.bean;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
/**
* Student实体类
* @author sjf0115
*
*/
public class Student implements BeanNameAware,BeanFactoryAware,InitializingBean,ApplicationContextAware, DisposableBean{
private String name;
private int age;
private String school;
// 构造器
public Student(){
System.out.println("constructor Student...");
}
public void setName(String name) {
this.name = name;
System.out.println("setName...");
}
 
public void setAge(int age) {
this.age = age;
System.out.println("setAge...");
}
 
public void setSchool(String school) {
this.school = school;
System.out.println("setSchool...");
}
 
public void myInit(){
System.out.println("init-method...");
}
public void myDestroy(){
System.out.println("destroy-method...");
}
// 来自于BeanNameAware接口
public void setBeanName(String arg0) {
System.out.println("setBeanName...[" + arg0 + "]");
}
// 来自于BeanFactoryAware接口
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("setBeanFactory...");
}
// 来自于InitializingBean
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet...");
}
// 来自于DisposableBean
public void destroy() throws Exception {
System.out.println("destroy...");
}
// 来自于ApplicationContextAware接口
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
System.out.println("setApplicationContext...");
}
@Override
public String toString() {
return "name:" + name + " age:" + age + " school:" + school;
}
}

然后是自定义一个MyBeanPostProcessor Bean,实现了 BeanPostProcessor接口。
   
   
package com.sjf.bean;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
/**
* 自定义BeanPostProcessor
* @author sjf0115
*
*/
public class MyBeanPostProcessor implements BeanPostProcessor{
public MyBeanPostProcessor(){
System.out.println("constructor MyBeanPostProcessor...");
}
// 来自于BeanPostProcessor接口
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("postProcessAfterInitialization...[" + arg1 + "]");
return arg0;
}
// 来自于BeanPostProcessor接口
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("postProcessBeforeInitialization...[" + arg1 + "]");
return arg0;
}
}

BeanPostProcessor接口包括2个方法postProcessAfterInitialization()和postProcessBeforeInitialization(),这两个方法的第一个参数都是要处理的Bean对象,第二个参数都是Bean的name。返回值也都是要处理的Bean对象。

接口中的两个方法都要将传入的bean返回,而不能返回null,如果返回的是null那么我们通过getBean方法将得不到目标。

一般ApplicationContext会自动检查是否在定义文件中有实现了BeanPostProcessor接口的类,如果有的话,Spring容器会在每个Bean(其他的Bean)被初始化之前和初始化之后,分别调用实现了BeanPostProcessor接口的类的postProcessAfterInitialization()方法和postProcessBeforeInitialization()方法,对Bean进行相关操作。刚开始 只定义了 Student 一个bean,并且实现了BeanPostProcessor接口,并不会调用 postProcessAfterInitialization()和postProcessBeforeInitialization()方法,这时ApplicationContext找不到 实现了BeanPostProcessor接口的类。 所以这里一个 Bean用来实现BeanPostProcessor接口,然后另一个Bean用来配合实现了BeanPostProcessor接口的Bean。

网友的疑问以及解答: http://bbs.csdn.net/topics/300121037

配置文件applicationContext.xml:

   
   
<?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 = "yoona" class = "com.sjf.bean.Student" scope="singleton" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="yoona"/>
<property name="age" value="24"/>
<property name="school" value="西电"/>
</bean>
<bean id = "myBeanPostProcessor" class="com.sjf.bean.MyBeanPostProcessor"/>
</beans>

测试代码:

   
   
package com.sjf.bean;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
/**
* 测试类
* @author sjf0115
*
*/
public class Test {
 
private static ApplicationContext context;
private static Student stu;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取Bean实例
stu = (Student)context.getBean("yoona");
// 3.调用listAllBook方法
System.out.println(stu.toString());
// 4.关闭容器
((ClassPathXmlApplicationContext) context).registerShutdownHook();
}
}

运行结果:
   
   
constructor MyBeanPostProcessor...
constructor Student...
setName...
setAge...
setSchool...
setBeanName...[yoona]
setBeanFactory...
setApplicationContext...
postProcessBeforeInitialization...[yoona]
afterPropertiesSet...
init-method...
postProcessAfterInitialization...[yoona]
nameyoona age24 school:西电
destroy...
destroy-method...
从这个运行结果中我们就可以看出一个Bean的生命周期:constructor( 实例化) -> setXXX( 填充属性) -> BeanName( BeanNameAware接口) -> BeanFactory( BeanFactoryAware接口) -> ApplicationContext( ApplicationContextAware接口 ) -> postProcessBeforeInitialization(BeanPostProcessor接口) -> afterPropertiesSet( InitializingBean接口 ) -> init-method(定制的初始化方法) -> postProcessAfterInitialization BeanPostProcessor接口  -> destroy( DisposableBean接口 ) -> destroy-method(定制的销毁方法


来源于:《Spring实战》


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring 生命周期Bean 生命周期是密切相关的。在 Spring 容器中,每个 Bean 都有一个完整的生命周期,即从实例化、依赖注入,到销毁的过程,Spring 容器为我们管理了这些过程。Bean 生命周期包括 Bean 的创建、初始化、使用和销毁。具体来说,Spring 生命周期包括以下阶段: 1.实例化 Bean:容器根据配置文件或注解等方式创建 Bean 的实例。 2.设置 Bean 的属性值:容器会将 Bean 的属性值注入到 Bean 中,这是 Bean 生命周期的第二个阶段。Spring 提供了两种常用方式来实现 Bean 的属性注入:构造函数注入和 Setter 方法注入。 3.调用 Bean 的初始化方法:在 Bean 的所有属性被设置之后,Spring 容器会调用 Bean 的初始化方法,这个方法可以是自定义的方法,也可以是 Spring 提供的初始化方法。 4.使用 Bean:在 Bean 初始化完成之后,Spring 容器会将 Bean 注入到需要使用它的地方,比如其他 Bean 或者 Controller 等。 5.销毁 Bean:当 Bean 不再需要时,Spring 容器会调用 Bean 的销毁方法,这个方法可以是自定义的方法,也可以是 Spring 提供的销毁方法。 Bean 生命周期Spring 生命周期的一部分,它描述了一个 Bean 在 Spring 容器中的生命周期Bean 生命周期包括以下阶段: 1.实例化 Bean:容器根据配置文件或注解等方式创建 Bean 的实例。 2.设置 Bean 的属性值:容器会将 Bean 的属性值注入到 Bean 中,这是 Bean 生命周期的第二个阶段。Spring 提供了两种常用方式来实现 Bean 的属性注入:构造函数注入和 Setter 方法注入。 3.调用 Bean 的初始化方法:在 Bean 的所有属性被设置之后,Spring 容器会调用 Bean 的初始化方法,这个方法可以是自定义的方法,也可以是 Spring 提供的初始化方法。 4.使用 Bean:在 Bean 初始化完成之后,Spring 容器会将 Bean 注入到需要使用它的地方,比如其他 Bean 或者 Controller 等。 5.销毁 Bean:当 Bean 不再需要时,Spring 容器会调用 Bean 的销毁方法,这个方法可以是自定义的方法,也可以是 Spring 提供的销毁方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值