Spring框架中Bean的生命周期

Spring框架提供了类似于C++中构造函数和析构函数的支持,分别叫做初始化方法和销毁方法。

初始化方法在对象创建制后并且完成了依赖注入之后调用的

销毁方法是在对象被容器销毁的时候创建的,开进行资源的释放工作。

首先我们创建一个Bean

这个Bean创建了两个方法init()destroy()方法,分别是用来做初始化方法和销毁方法。

qiudawei115.lifeCycle. LifeCycleBean.java

package qiudawei115.lifeCycle;

 

public class LifeCycleBean {

    String message;

 

    public String getMessage() {

       return message;

    }

 

    public void setMessage(String message) {

       this.message = message;

    }

   

    public void init(){

       System.out.println("LifeCycleBean对象初始化...");

       System.out.println(this.getMessage());

    }

   

    public void destroy(){

       System.out.println("LifeCycleBean对象被销毁...");

       message="GoodBye World!";

    }

}

在配置文件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-2.0.xsd">

<!-- Hello World! -->

<bean id="helloWorld" class="qiudawei115.base.HelloWorld">

    <property name="message" value="Hello World!"></property>

</bean>

<!-- Constructor DI -->

<bean id="fb" class="qiudawei115.constructor.FirstBean"></bean>

<bean id="sb" class="qiudawei115.constructor.SecondBean"></bean>

<bean id="cb" class="qiudawei115.constructor.ConstructorBean">

    <constructor-arg>

       <ref bean="fb"/>

    </constructor-arg>

    <constructor-arg>

       <ref bean="sb"/>

    </constructor-arg>

</bean>

<!-- Setter DI -->

<bean id="sfb" class="qiudawei115.setter.FirstBean"></bean>

<bean id="ssb" class="qiudawei115.setter.SecondBean"></bean>

<bean id="setterBean" class="qiudawei115.setter.SetterBean">

    <property name="sfb" ref="sfb"></property>

    <property name="ssb" ref="ssb"></property>

</bean>

<!-- LookUp DI -->

<bean id="pb" class="qiudawei115.lookUp.PrototypeBean" scope="prototype"></bean>

<bean id="lsb" class="qiudawei115.lookUp.SingletonBean" >

    <property name="pb" ref="pb"></property>

    <lookup-method name="getPrototypeBean" bean="pb"/>

</bean>

<!-- LifeCycle Bean -->

<bean id="lcb" class="qiudawei115.lifeCycle.LifeCycleBean" init-method="init" destroy-method="destroy">

    <property name="message" value="Hello World!"></property>

</bean>

</beans>

测试Bean

qiudawei115.lifeCycle. LifeCycleBeanMain.java

package qiudawei115.lifeCycle;

 

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

public class LifeCycleBeanMain {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       ClassPathResource resource=new ClassPathResource("applicationContext.xml");

       XmlBeanFactory beanFactory=new XmlBeanFactory(resource);

      

       LifeCycleBean lcb=(LifeCycleBean)beanFactory.getBean("lcb");

       //System.out.println(lcb.getMessage());

       //lcb.destroy();

       beanFactory.destroySingletons();

       System.out.println(lcb.getMessage());

    }

}

输出结果为:

LifeCycleBean对象初始化...

Hello World!

LifeCycleBean对象被销毁...

GoodBye World!

这就是SpringBean的生命周期。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值