Spring - Bean生命周期



Spring Bean的生命周期很容易理解。当实例化一个Bean时,可能需要执行一些初始化操作以使其处于可用状态。类似地,当不再需要Bean并将其从容器中移除时,可能需要进行一些清理操作。

虽然,在Bean实例化和销毁之间发生的幕后活动有一系列列表,但本章将只讨论两个重要的Bean生命周期回调方法,这些方法在Bean初始化和销毁时是必需的。

要为Bean定义设置和拆卸,我们只需使用init-method和/或destroy-method参数声明<bean>。init-method属性指定一个方法,该方法将在Bean实例化后立即被调用。类似地,destroy-method指定一个方法,该方法在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注册一个关闭钩子。这样做可以确保优雅地关闭并调用单例Bean的相关destroy方法,以便释放所有资源。

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

示例

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

步骤 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个名为com.tutorialspoint的包。
2 使用添加外部JAR选项添加所需的Spring库,如Spring Hello World示例章节中所述。
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文件的内容。在这里,您需要注册一个关闭钩子registerShutdownHook()方法,该方法在AbstractApplicationContext类上声明。这将确保优雅地关闭并调用相关的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上都声明init-methoddestroy-method。相反,框架提供了使用<beans>元素上的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>
广告

© . All rights reserved.