spring bean的生命周期(4)

来源:互联网 发布:网络借贷细则 编辑:程序博客网 时间:2024/05/22 14:02

spring bean生命周期包含bean定义、初始化、使用及销毁等阶段。
bean定义指的是在xml文件中配置bean或bean类中注解bean,bean使用即从springcontext中获取已经初始化的bean并应用的过程,本文主要介绍bean的初始化及销毁阶段。

一、配置实现spring bean初始化、销毁的方式

1、xml文件中配置bean的init-method属性和destroy-method属性

xml配置文件:

<bean name="beanScope" class="com.jsun.test.springDemo.BeanScope" init-method="init_m" destroy-method="destroy_m"></bean>

BeanScope类:

public class BeanScope {    public BeanScope(){        System.out.println("BeanScope bean 实例化");    }    public void init_m(){        System.out.println("init-method 初始化");    }    public void destroy_m(){        System.out.println("destroy-method 销毁");    }}

从上面执行结果可以看出,bean的实例化构造器要先于初始化方法执行。

同时需要注意的是:如果bean中配置init-method和init-destroy属性,那么在bean类中必须提供对应的初始化和销毁方法,否则将抛出初始化异常:Exception encountered during context initialization

2、配置全局默认的初始化和销毁方法

<beans ... default-init-method="init_d_m" default-destroy-method="destroy_d_m"><bean name="beanScope" class="com.jsun.test.springDemo.BeanScope"></bean></beans>BeanScope类:public class BeanScope {    public BeanScope(){        System.out.println("BeanScope bean 实例化");    }    public void init_d_m(){        System.out.println("default-init-method 初始化");    }    public void destroy_d_m(){        System.out.println("default-destroy-method 销毁");    }}

执行结果与上面情况一致,如果同时配置了bean的初始化、销毁和全局的初始化、销毁方法,那么bean的初始化、销毁方法将覆盖全局配置。【全局配置被覆盖】

另外,配置了全局的初始化、销毁方法,bean类中全局的初始化和销毁方法可以不提供,bean可以被正常初始化。【全局方法可以没有】

3、实现org.springframework.beans.factory.DisposableBean和org.springframework.beans.factory.InitializingBean接口,覆盖实现afterPropertiesSet()和destroy()方法

BeanScope类:

public class BeanScope implements InitializingBean,DisposableBean {    public BeanScope(){        System.out.println("BeanScope 实例化");    }    @Override    public void afterPropertiesSet() throws Exception {        System.out.println("实现接口InitializingBean afterPropertiesSet()初始化");    }    @Override    public void destroy() throws Exception {        System.out.println("实现接口DisposableBean destroy()销毁");    }}

执行结果与上相同

注:如果配置了bean的init-method和init-destroy属性(或同时配置全局的default-init-method和default-destroy-method属性),并实现了上述的初始化和销毁接口,接口的初始化和销毁方法将先于配置的初始化和销毁方法

4、注解配置方式

在指定方法上加上@PostConstruct或@PreDestroy注解来指定该方法作为初始化或销毁方法来使用。

   <!-- 开启注解 -->    <context:annotation-config/>        <!-- 配置bean -->    <bean name="beanScope" class="com.jsun.test.springDemo.BeanScope"></bean>public class BeanScope{    public BeanScope(){        System.out.println("BeanScope bean 实例化");    }    //添加@PostConstruct注解,指定当前方法作为初始化方法    @PostConstruct    public void init(){        System.out.println("@PostConstruct注解 初始化");    }    //添加@PreDestroy注解,指定当前方法作为销毁方法    @PreDestroy    public void destroy(){        System.out.println("@PreDestroy注解 销毁");    }}

二、bean的作用域与生命周期之间的关系以及初始化时机

1、初始化bean实例是时机与bean作用域的关系

(1)、当scope=singleton,即默认情况,会在容器初始化时实例化、初始化。

但如果指定bean节点的lazy-init=”true”,来延迟bean的实例化、初始化,当第一次获取bean时才会初始化bean。

<!-- 使用默认作用域singleton --><bean name="beanScope" class="com.jsun.test.springDemo.BeanScope" init-method="init_m" destroy-method="destroy_m" ></bean>public class BeanScope{    public BeanScope(){        System.out.println("BeanScope bean 实例化");    }    public void init_m(){        System.out.println("init-method 初始化");    }    public void destroy_m(){        System.out.println("destroy-method 销毁");    }}    @Test    public void testScope(){        System.out.println("获取bean之前");        BeanScope beanScope = super.getBean("beanScope");        System.out.println("获取bean的hashCode:"+beanScope.hashCode());    }

为bean添加lazy-init属性:

<bean name="beanScope" class="com.jsun.test.springDemo.BeanScope" init-method="init_m" destroy-method="destroy_m" lazy-init="true"></bean>

(2)、当scope=prototype时,也会延迟初始化bean,相当于设置lazy-init=”true”,即第一次请求该bean时才初始化(如调用getBean()方法时);并且当spring容器关闭时,destroy()销毁方法也不会被调用。

<bean name="beanScope" class="com.jsun.test.springDemo.BeanScope" init-method="init_m" destroy-method="destroy_m" scope="prototype"></bean>

//spring容器关闭了,但是destroy()销毁方法并没有被执行

原因:对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法。但对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。

0 0
原创粉丝点击