Spring(十二):IOC容器中Bean的生命週期方法

weixin_34198881發表於2018-05-24

IOC容器中Bean的生命週期方法

1)Spring IOC容器可以管理Bean的宣告週期,Spring允許在Bean生命週期的特定點執行定製的任務。

2)Spring IOC容器對Bean的生命週期進行管理的過程:

--- 通過構造器或工廠方法建立Bean例項;

--- 為Bean的屬性設定值和對其他Bean的引用;

--- Bean可以使用

--- 當容器關閉是,呼叫Bean的銷燬方法

3)在Bean的宣告裡設定init-method和destroy-method屬性,為Bean指定初始化和銷燬方法。

演示:

第一步:建立一個Java Project,並匯入以下包:

第二步:在src下新增com.dx.spring.beans.cycle.Member.java和spring bean配置檔案bean-cycle.xml

Member.java

package com.dx.spring.beans.cycle;

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

    private String name;

    public void setName(String name) {
        System.out.println("setName " + name);
        this.name = name;
    }

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

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

注意:Member中的initMethod和destroyMethod兩個方法名字是任意的。

bean-cycle.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="member" class="com.dx.spring.beans.cycle.Member" init-method="initMethod" destroy-method="destroyMethod">
        <property name="name" value="member1"></property>
    </bean>
</beans>

第三步:使用IOC容器管理Bean並關閉IOC容器檢視列印日誌:

package com.dx.spring.beans.cycle;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean-cycle.xml");
        
        Member member = (Member) ctx.getBean("member");
        System.out.println(member);

        ctx.close();                // 關閉IOC容器
    }
}

列印資訊如下:

資訊: Loading XML bean definitions from class path resource [bean-cycle.xml]
Member constructor ... 
setName member1
init method
com.dx.spring.beans.cycle.Member@4f7d0008
May 24, 2018 9:41:33 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
資訊: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Thu May 24 21:41:33 CST 2018]; root of context hierarchy
despose method

建立Bean後置處理器

1)Bean後置處理器允許在呼叫初始化方法前後對Bean進行額外的處理;

2)Bean後置處理器對IOC容器裡的所有Bean例項逐一進行處理,而非單一例項。其經典應用是:檢查Bean屬性的正確性或根據特定的標準更改Bean的屬性。

3)對Bean後置處理器而言,需要實現abstract interface org.springframework.beans.factory.config.BeanPostProcessor抽象介面。在初始化方法被呼叫前後,Spring將把每個Bean例項分別傳遞給上述介面的以下兩個方法:

演示:

第一步:新增com.dx.spring.beans.cycle.MyBeanPostProcessor.java使其實現抽象介面類BeanPostProcessor

package com.dx.spring.beans.cycle;

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("postProcessBeforeInitialization:" + beanName + "," + bean);
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

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

第二步:修改spring-cycle.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="member" class="com.dx.spring.beans.cycle.Member" init-method="initMethod" destroy-method="destroyMethod">
        <property name="name" value="member1"></property>
    </bean>
    <!-- 注意:這裡不需要指定id -->
    <bean class="com.dx.spring.beans.cycle.MyBeanPostProcessor"></bean>
</beans>

第三步:執行Main測試,檢視列印資訊:

資訊: Loading XML bean definitions from class path resource [bean-cycle.xml]
Member constructor ... 
setName member1
postProcessBeforeInitialization:member,com.dx.spring.beans.cycle.Member@1a968a59
init method
postProcessAfterInitializationmember,com.dx.spring.beans.cycle.Member@1a968a59
com.dx.spring.beans.cycle.Member@1a968a59
May 24, 2018 9:46:07 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
資訊: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Thu May 24 21:46:06 CST 2018]; root of context hierarchy
despose method

 

相關文章