Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring Bean 生命周期
Spring Bean 生命周期
作者:--    发布时间:2019-11-20

理解 spring bean 的生命周期很容易。当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态。同样,当 bean 不再需要,并且从容器中移除时,可能需要做一些清除工作。

尽管还有一些在 bean 实例化和销毁之间发生的活动,但是本章将只讨论两个重要的生命周期回调方法,它们在 bean 的初始化和销毁的时候是必需的。

为了定义安装和拆卸一个 bean,我们只要声明带有 init-method 和/或 destroy-method 参数的 。init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。

bean的生命周期可以表达为:bean的定义——bean的初始化——bean的使用——bean的销毁

初始化回调

org.springframework.beans.factory.initializingbean 接口指定一个单一的方法:

void afterpropertiesset() throws exception;

因此,你可以简单地实现上述接口和初始化工作可以在 afterpropertiesset() 方法中执行,如下所示:

public class examplebean implements initializingbean {
   public void afterpropertiesset() {
      // do some initialization work
   }
}

在基于 xml 的配置元数据的情况下,你可以使用 init-method 属性来指定带有 void 无参数方法的名称。例如:

<bean id="examplebean" 
         class="examples.examplebean" init-method="init"/>

下面是类的定义:

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

销毁回调

org.springframework.beans.factory.disposablebean 接口指定一个单一的方法:

void destroy() throws exception;

因此,你可以简单地实现上述接口并且结束工作可以在 destroy() 方法中执行,如下所示:

public class examplebean implements disposablebean {
   public void destroy() {
      // do some destruction work
   }
}

在基于 xml 的配置元数据的情况下,你可以使用 destroy-method 属性来指定带有 void 无参数方法的名称。例如:

<bean id="examplebean"
         class="examples.examplebean" destroy-method="destroy"/>

下面是类的定义:

public class examplebean {
   public void destroy() {
      // do some destruction work
   }
}

如果你在非 web 应用程序环境中使用 spring 的 ioc 容器;例如在丰富的客户端桌面环境中;那么在 jvm 中你要注册关闭 hook。这样做可以确保正常关闭,为了让所有的资源都被释放,可以在单个 beans 上调用 destroy 方法。

建议你不要使用 initializingbean 或者 disposablebean 的回调方法,因为 xml 配置在命名方法上提供了极大的灵活性。

例子

我们在适当的位置使用 eclipse ide,然后按照下面的步骤来创建一个 spring 应用程序:

步骤 描述
1 创建一个名称为 springexample 的项目,并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint
2 使用 add external jars 选项,添加所需的 spring 库,解释见 spring hello world example 章节。
3 com.tutorialspoint 包中创建 java 类 helloworldmainapp
4 src 文件夹中创建 beans 配置文件 beans.xml
5 最后一步是创建的所有 java 文件和 bean 配置文件的内容,并运行应用程序,解释如下所示。

这里是 helloworld.java 的文件的内容:

package com.tutorialspoint;

public class helloworld {
   private string message;

   public void setmessage(string message){
      this.message  = message;
   }
   public void getmessage(){
      system.out.println("your message : " + message);
   }
   public void init(){
      system.out.println("bean is going through init.");
   }
   public void destroy(){
      system.out.println("bean will destroy now.");
   }
}

下面是 mainapp.java 文件的内容。在这里,你需要注册一个在 abstractapplicationcontext 类中声明的关闭 hook 的 registershutdownhook() 方法。它将确保正常关闭,并且调用相关的 destroy 方法。

package com.tutorialspoint;
import org.springframework.context.support.abstractapplicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class mainapp {
   public static void main(string[] args) {
      abstractapplicationcontext context = new classpathxmlapplicationcontext("beans.xml");
      helloworld obj = (helloworld) context.getbean("helloworld");
      obj.getmessage();
      context.registershutdownhook();
   }
}

下面是 init 和 destroy 方法必需的配置文件 beans.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-3.0.xsd">

   <bean id="helloworld" 
       class="com.tutorialspoint.helloworld"
       init-method="init" destroy-method="destroy">
       <property name="message" value="hello world!"/>
   </bean>

</beans>

一旦你创建源代码和 bean 配置文件完成后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:

bean is going through init.
your message : hello world!
bean will destroy now.

默认的初始化和销毁方法

如果你有太多具有相同名称的初始化或者销毁方法的 bean,那么你不需要在每一个 bean 上声明初始化方法销毁方法。框架使用 元素中的 default-init-methoddefault-destroy-method 属性提供了灵活地配置这种情况,如下所示:

<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-3.0.xsd"
    default-init-method="init" 
    default-destroy-method="destroy">

   <bean id="..." class="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

</beans>
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册