spring基础知识 (11):bean的生命周期

  • Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务.
  • Spring IOC 容器对 Bean 的生命周期进行管理的过程:
    • 创建 Bean 实例
    • 为 Bean 的属性赋值
    • 调用 Bean 的初始化方法init-method
    • Bean 可以使用了
    • 当容器关闭时, 调用 Bean 的销毁方法destroy-method
  • 在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法.

看下面这个例子

  • 创建一个Car类
package com.spring.cycle;

public class Car {

    private String name;

    public Car() {
        System.out.println("创建bean...");
    }
    private void init() {
        System.out.println("调用init...");
    }
    private void destroy() {
        System.out.println("调用destroy...");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("设置属性...");
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
}

创建了init和destroy方法,这个名字没有特定意义,可以随便取。

  • 配置bean:
<!-- 使用spel引用类的静态属性 -->
<bean id="car" class="com.spring.cycle.Car" init-method="init" destroy-method="destroy">
    <property name="name" value="BMW"></property>
</bean>

在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法。
方法名可以随便取,不一定非要取名为init和destroy

  • 测试类:
//由于ApplicationContext类没有close方法,这里使用其子类
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");

Car car = (Car) ctx.getBean("car");
System.out.println(car);
ctx.close();

由于ApplicationContext类没有close方法,这里使用其子类ClassPathXmlApplicationContext

  • 测试结果:
    这里写图片描述

bean后置处理器

Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理.
定义Bean 后置处理器类需要实现BeanPostProcessor接口,实现两个方法:
postProcessAfterInitialization :init方法执行前执行
postProcessBeforeInitialization : init方法执行后执行
相当于在bean生命周期中加入了两个阶段。
下面看例子:

  • 创建一个后置处理器类MyBeanPostProcessor:
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
         System.out.println("postProcessAfterInitialization:"+bean+":"+beanName);
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("postProcessBeforeInitialization:"+bean+":"+beanName);
        return bean;
    }

}
  • 将这个后置处理器加入到bean容器中:
<bean class="com.spring.cycle.MyBeanPostProcessor"></bean>
  • 测试:
//由于ApplicationContext类没有close方法,这里使用其子类
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");

Car car = (Car) ctx.getBean("car");
System.out.println("使用bean:"+car);
ctx.close();

这里写图片描述

  • Spring IOC 容器对 Bean 的生命周期进行管理的过程:
    • 创建 Bean 实例
    • 为 Bean 的属性赋值
    • 将 Bean 实例传递给 Bean 后置处理器的 postProcessBeforeInitialization方法
    • 调用 Bean 的初始化方法
    • 将 Bean 实例传递给 Bean 后置处理器的 postProcessAfterInitialization方法
    • Bean 可以使用了
    • 当容器关闭时, 调用 Bean 的销毁方法

本系列参考视频教程: http://edu.51cto.com/course/1956.html

猜你喜欢

转载自blog.csdn.net/abc997995674/article/details/80284976