歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring重點—— IOC 容器中 Bean 的生命周期

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

日期:2017/3/1 9:13:29   编辑:Linux編程

一、理解 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 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved