Spring—DisposableBean

来源:互联网 发布:淘宝优惠券二合一平台 编辑:程序博客网 时间:2024/06/04 20:13

在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等。 
周末对这两个方法进行了一点学习和整理,倒也不是专门为了这两个方法,而是在巩固spring相关知识的时候提到了,然后感觉自己并不是很熟悉这个,便好好的了解一下。 
根据特意的去了解后,发现实际上可以有三种方式来实现init method和destroy method。 
要用这两个方法,自然先要知道这两个方法究竟是干嘛用的。而从字面意思就很容易理解,一个是加载,一个是销毁。 
下边就正式代码演示三种创建方式: 
一、@Bean注解方式: 
首先要创建一个至少拥有两个方法的类,一个方法充当init method,另一个充当destroy method。

package springTest2;public class Test1 {    public void init() {        System.out.println("this is init method1");    }    public Test1() {        super();        System.out.println("构造函数1");    }    public void destroy() {        System.out.println("this is destroy method1");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这里很显然只是一个普通的java类,拥有一个无参构造和另外两个方法。 
需要注意的是,这里的init和destroy两个方法名实际上是可以随意取得,不叫这个也没有问题,只不过算是一种约定俗称,一般都是这样叫。 
另外我们也知道,这个构造方法也是可以不要的,因为会隐式的自动创建,但是为了更清楚的看到init和destroy是什么时候执行,我们就显示的写出来。 
创建好了这个类,我们就可以使用@Bean注解的方式指定两个方法,以让他们生效。

package springTest2;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("springTest2")public class ConfigTest {    @Bean(initMethod = "init", destroyMethod = "destroy")    Test1 test1() {        return new Test1();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这里边的@Configguration注解是告诉spring这个类是一个配置类,相当于我们的xml文件,@ComponentScan则是指定需要spring来扫描的包,相当于xml中的context:component-scan属性。 
而@Bean后边的initMethod和destroyMethod就是在声明这是一个baen的同时指定了init和destroy方法,方法名从功能实现上来说可以随意。 
到这里我们就已经用第一种方式写好了,为了验证成功与否,再写一个main方法验证一下:

package springTest2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainTest {    public static void main(String[] args) {        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigTest.class);                System.out.println("#################################");        context.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行之后结果如图: 
这里写图片描述 
根据打印顺序可以看到,首先是构造函数,也就是创建了bean,紧接着执行了init,然后再context.close要销毁bean之前又执行了destroy。

二、JSR-250注解的方式(需要导入jsr250-api的jar包): 
首先依然是创建一个拥有构造方法在内的三个方法的java类:

package springTest2;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;public class Test2 {    @PostConstruct    public void init() {        System.out.println("this is init method2");    }    public Test2() {        super();        System.out.println("构造函数2");    }    @PreDestroy    public void destroy() {        System.out.println("this is destroy method2");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

很显然,这里和上一个类不同的是,在init和destroy方法上加入了两个注解,@PostConstruct和上边@Bean后的initMethod相同,而@PreDestroy则是和destroyMethod做用相同。 
既然这里有了区别,已经指定了init method和destroy method,那么后边声明bean的时候自然也会有不同,也就不需要再指定一遍:

package springTest2;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("springTest2")public class ConfigTest {    @Bean    Test2 test2() {        return new Test2();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

所以,如上代码中只需要简单的声明这是一个bean就可以了,类上边的两个注解和上一个例子中的意思相同。 
再测试一下:

package springTest2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainTest {    public static void main(String[] args) {        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigTest.class);                System.out.println("#################################");        context.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

结果如下: 
这里写图片描述

三、xml配置的方式: 
这种方式实际上是和第一种对应的,只不过细节上略有改变而已,首先,创建的java类完全一样:

package springTest2;public class Test3 {    public void init() {        System.out.println("this is init method3");    }    public Test3() {        super();        System.out.println("构造函数3");    }    public void destroy() {        System.out.println("this is destroy method3");    }    public void test() {        System.out.println("testttttttt");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

不同的地方就在于,第一个例子中是使用注解告诉spring这个类相当于一个配置文件,而这里则是实实在在的配置文件spring.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="initOrDestroyTest" class="springTest2.Test3" init-method="init" destroy-method="destroy"></bean></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这个配置大概也能算是spring.xml中最简单的一个配置了吧,除开必要的文件头,就只有一个bean,而且bean里边也只有id,calss和init以及destroy方法。 
因为简单,所以一目了然,id只是为了其他地方引用,class是指定这个bean对应的类,而后边两个属性则和用@Bean声明时一模一样。 
因为这里声明bean和指定两个方法是用的xml配置,因此在测试的时候也就需要稍微有一点点改变:

package springTest2;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainTest {    public static void main(String[] args) {        ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("spring.xml");        System.out.println("#################################");        context1.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

区别在于这里直接加载了配置文件,而不是java类,使用的是ClassPathxXmlApplicationContext而不是AnnotationConfigApplicationContext。 
结果如下: 
这里写图片描述

这里需要说明的一点是,在实际的web应用使用时,可以在web.xml中使用类似下边的配置来加载bean,实现init method:

<servlet-name>dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>      <servlet-name>dispatcher</servlet-name>      <url-pattern>/</url-pattern>    </servlet-mapping> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

然后启动tomcat结果如下: 
这里写图片描述 
这里边没有调用destroy method,原因是spring本身代码就需要我们手动调用销毁bean的方法,像前边的几个例子中的context.close就是。 
如果不手动调用这个方法,bean就不会被销毁,也就不会去调用destroy method,这也就是为何这里在web.xml中配置后,启动tomcat 只打印了构造函数和init方法中的内容。

例子都是很简单的,而通过简单的例子对比可能能更进一步理解相关的知识,理解了才能在实际应用中更好的进行选择和集成。

参考:http://blog.csdn.net/tuzongxun/article/details/53580695


=============================================================================================================================


Spring 中Bean的生命周期与InitializingBean和DisposableBean

 

在内部,Spring使用BeanPostProcessor来处理它能找到的标志接口以及调用适当的方法,如果你需要自定义的特性或者其他的spring没有提供的生命周期的行为,你可以实现自己的BeanPostProcessor

 

 

initializingBean/init-method

 

Spirng的InitializingBean为bean提供了定义初始化方法的方式
InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()
实现org.springframework.beans.factory.InitializingBean接口允许一个bean在它的所有必须属性被BeanFactory设置后,来执行初始化的工作,InitialzingBean仅仅指定了一个方法。

注意通常InitializingBean接口的使用是能够被避免的,(不鼓励使用,因为没有必要把代码通Spring耦合起来) Bean的定义支持指定一个普通的初始化方法,在使用XmlBeanFactory的情况下,可以通过指定init-method属性来完成

 

<baan id="exampleInitBean" class="example.ExampleBean" init-method="init"/>

public class ExampleBean { 
   public void init(){ 
     //do some initialization work; 
    } 
}

同下面的是一样的 
<bean id="exampleInitBean" class="example.AnotherExampleBean" />

public class AnotherExampleBean implements InitializingBean
     public void afterPropertiesSet(){ 
         //do some initialization work

   } 

但却不把代码耦合于spring中

 


DisposableBean/destroy-method

实现org.springframework.beans.factory.DisposableBean接口的bean允许在容器销毁该bean的时候获得一次回调。DisposableBean接口也只规定了一个方法:

void destroy() throws Exception;
通常,要避免使用DisposableBean标志接口而且不鼓励使用该接口,因为这样会将代码与Spring耦合在一起,有一个可选的方案是,在bean定义中指定一个普通的析构方法,然后在XML配置文件中通过指定destroy-method属性来完成。如下面的定义所示:

<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean {

    public void cleanup() {
        // do some destruction work (like releasing pooled connections)
    }
}
...效果与下面完全一样...

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean {

    public void destroy() {
        // do some destruction work (like releasing pooled connections)
    }
}

... 但是没有将代码与Spring耦合在一起。

 


缺省的初始化和析构方法<beans default-init-method="init">

如果有人没有采用Spring所指定的InitializingBean和DisposableBean回调接口来编写初始化和析构方法回调,会发现自己正在编写的方法,其名称莫过于init(), initialize(),dispose()等等。这种生命周期回调方法的名称最好在一个项目范围内标准化,这样团队中的开发人员就可以使用同样的方法名称,并且确保了某种程度的一致性。

Spring容器通过配置可以实现对每个 bean初始化时的查找和销毁时的回调调用。这也就是说,一个应用的开发者可以借助于初始化的回调方法init() 轻松的写一个类(不必想XML配置文件那样为每个bean都配置一个'init-method="init"'属性)。Spring IoC容器在创建bean的时候将调用这个方法 (这和之前描述的标准生命周期回调一致)。

为了完全弄清如何使用该特性,让我们看一个例子。出于示范的目的,假设一个项目的编码规范中约定所有的初始化回调方法都被命名为init()而析构回调方法被命名为destroy()。遵循此规则写成的类如下所示:

public class DefaultBlogService implements BlogService {

    private BlogDao blogDao;

    public void setBlogDao(BlogDao blogDao) {
        this.blogDao = blogDao;
    }

    // this is (unsurprisingly) the initialization callback method
    public void init() {
        if (this.blogDao == null) {
            throw new IllegalStateException("The [blogDao] property must be set.");
        }
    }
}
<beans default-init-method="init">

    <bean id="blogService" class="com.foo.DefaultBlogService">
        <property name="blogDao" ref="blogDao" />
    </bean>

</beans>

 

注意在顶级的<beans/>元素中的'default-init-method'属性。这个属性的含义是Spring IoC容器在bean创建和装配的时候会将'init'方法作为实例化回调方法。如果类有这个方法,则会在适当的时候执行。

销毁回调方法配置是相同的 (XML配置),在顶级的<beans/>元素中使用 'default-destroy-method' 属性。

使用这个功能可以把你从位每个bean指定初始化和销毁回调的繁杂工作中解救出来。为了一致性,应该强制性的为初始化和销毁回调方法采用一致的命名规则。

当已经存在的类的初始化方法的命名规则与惯例有差异的时候,你应该始终使用<bean/>元素中的'init-method'和'destroy-method'属性(在XML配置中)来覆盖默认的方式。

最后,请注意Spring容器保证在bean的所有依赖都满足后立即执行配置的初始化回调。这意味着初始化回调在原生bean上调用,这也意味着这个时候任何诸如AOP拦截器之类的将不能被应用。一个目标bean是首先完全创建,然后才应用诸如AOP代理等拦截器链。注意,如果目标bean和代理是分开定义了,你的代码甚至可以绕开代理直接和原生bean通信。因此,在初始化方法上使用拦截器将产生未知的结果,因为这将目标bean和它的代理/拦截器的生命周期绑定并且留下了和初始bean直接通信这样奇怪的方式。


参考:http://blog.csdn.net/LJHABC1982/article/details/6092490




阅读全文
0 0
原创粉丝点击