Spring - Bean Life Cycle

Spring bean的生命周期很容易理解.实例化bean时,可能需要执行一些初始化以使其进入可用状态.类似地,当不再需要bean并从容器中删除时,可能需要进行一些清理.

尽管如此,在时间之间发生了一系列活动本章将讨论bean实例化及其破坏,本章仅讨论两个重要的bean生命周期回调方法,这些方法在bean初始化及其销毁时是必需的.

定义设置和拆除一个bean,我们简单地声明< bean>使用 initmethod 和/或 destroy-method 参数. init-method属性指定在实例化时立即在bean上调用的方法.类似地,destroymethod指定在从容器中删除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 属性指定方法的名称有无空的无争论签名.例如 :

<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 属性指定方法的名称有无空的无争论签名.例如 :

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

以下是类定义 :

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

如果你在非Web应用程序环境中使用Spring的IoC容器;例如,在富客户端桌面环境中,您使用JVM注册关闭挂钩.这样做可确保正常关闭并在单例bean上调用相关的destroy方法,以便释放所有资源.

建议您不要使用InitializingBean或DisposableBean回调,因为XML配置在命名方法方面提供了很大的灵活性.

示例

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 :

步骤描述
1创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.it1352./td>
2使用添加所需的Spring库添加外部JAR 选项,如 Spring Hello World示例章节中所述.
3下创建Java类 HelloWorld MainApp com.it1352包.
4 src 文件夹下创建Beans配置文件 Beans.xml .
5最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述.

以下是 HelloWorld.java 文件的内容 :

package com.it1352; 
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类上声明的shutdown hook registerShutdownHook()方法.这将确保正常关闭并调用相关的销毁方法.

package com.it1352; 
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 = "https://img01.yuandaxia.cn/Content/img/tutorials/spring/"
   xmlns:xsi = "https://img01.yuandaxia.cn/Content/img/tutorials/spring/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   https://img01.yuandaxia.cn/Content/img/tutorials/spring/spring-beans-3.0.xsd">

   <bean id = "helloWorld" class = "com.IT屋.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有初始化和/或使用相同的方法销毁name,您不需要在每个bean上声明 init-method destroy-method .相反,该框架提供了使用< beans>上的 default-init-method default-destroy-method 属性来配置此类情况的灵活性.元素如下 :

<beans xmlns = "https://img01.yuandaxia.cn/Content/img/tutorials/spring/"
   xmlns:xsi = "https://img01.yuandaxia.cn/Content/img/tutorials/spring/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   https://img01.yuandaxia.cn/Content/img/tutorials/spring/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>