Spring重点—— IOC 容器中 Bean 的生命周期

一、理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助。

二、在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理。

三、在没有添加后置处理器的情况下 Bean 的生命周期

1.通过构造器或工厂方法创建 Bean 的实例

2.为 Bean 的属性设置值好对其他 Bean 的引用

3.调用 Bean 的初始化方法

4.Bean 可以使用了

5.当容器关闭时,调用 Bean 的销毁方法

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

例如:

 
/**
 * @author solverpeng
 * @create 2016-07-18-20:42
 */
public class Life {
    private String lifeName;

    public void setLifeName(String lifeName) {
        System.out.println("setLifeName....");
        this.lifeName = lifeName;
    }

    public Life() {
        System.out.println("constructor....");
    }

    public void initMethod() {
        System.out.println("initMethod....");
    }

    public void destroyMethod() {
        System.out.println("destroyMethod....");
    }

    public void targetMethod() {
        System.out.println("targetMethod....");
    }


}
 

spring-config.xml

<bean class="com.linuxidc.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
  <property name="lifeName" value="myLife"/>
bean>

Test

@Test
public void test03() {
  Life life = ctx.getBean(Life.class);
  life.targetMethod();
}

控制台输出:

constructor….
setLifeName….
initMethod….
targetMethod….

四、Bean 后置处理器

1.Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理。

2.Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理。

3.具体使用:需要实现 BeanPostProcessor 接口,实现 postProcessBeforeInitialization(Object bean, String beanName) 和 postProcessAfterInitialization(Object bean, String beanName) 两个方法。

分别在 初始化方法前后被调用。

五、添加 Bean 后置处理器后的 Bean 的生命周期

1.通过构造器或工厂方法创建 Bean 的实例

2.为 Bean 的属性设置值和对其他 Bean 的引用

3.将 Bean 实例传递给 bean 后置处理器的 postProcessBeforeInitialization() 方法

4.调用 Bean 的初始化方法

5.将 Bean 实例传递给 bean 后置处理器的 postProcessAfterInitialization() 方法。

6.使用 Bean

7.当容器关闭时,调用 Bean 的销毁方法。

例如:

 
/**
 * @author solverpeng
 * @create 2016-07-18-20:42
 */
public class Life {
    private String lifeName;

    public void setLifeName(String lifeName) {
        System.out.println("setLifeName....");
        this.lifeName = lifeName;
    }

    public Life() {
        System.out.println("constructor....");
    }

    public void initMethod() {
        System.out.println("initMethod....");
    }

    public void destroyMethod() {
        System.out.println("destroyMethod....");
    }

    public void targetMethod() {
        System.out.println("targetMethod....");
    }


}
 

Life.java

 
/**
 * @author solverpeng
 * @create 2016-07-18-20:58
 */
public class MyBeanPostProcessor implements BeanPostProcessor{
    
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof Life) {
            System.out.println("life's postProcessBeforeInitialization....");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof Life) {
            System.out.println("life's postProcessAfterInitialization....");
        }
        return bean;
    }
}
 
 

MyBeanPostProcessor.java

<bean class="com.linuxidc.spring.processor.MyBeanPostProcessor"/>

<bean class="com.linuxidc.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
  <property name="lifeName" value="myLife"/>
bean>

Test

@Test
public void test03() {
  Life life = ctx.getBean(Life.class);
  life.targetMethod();
}

控制台输出:

constructor….
setLifeName….
life’s postProcessBeforeInitialization….
initMethod….
life’s postProcessAfterInitialization….
targetMethod….

Spring中如何配置Hibernate事务 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基于 Spring 设计并实现 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成实例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 进行单元测试 http://www.linuxidc.com/Linux/2013-09/89913.htm

运用Spring注解实现Netty服务器端UDP应用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的详细介绍请点这里
Spring 的下载地址请点这里