Spring框架中Bean的生命周期

    技术2022-05-11  34

    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)