目录

Spring - Bean Life Cycle

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

虽然,在bean实例化及其销毁之间存在幕后发生的活动列表,但本章仅讨论两个重要的bean生命周期回调方法,这些方法在bean初始化及其销毁时是必需的。

要定义bean的设置和拆卸,我们只需使用initmethod和/或destroy-method参数声明<bean>。 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属性指定具有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配置在命名方法方面提供了很大的灵活性。

例子 (Example)

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

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3com.iowiki包下创建Java类HelloWorldMainApp
4src文件夹下创建Beans配置文件Beans.xml
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

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

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

package com.iowiki;
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.iowiki.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>
↑回到顶部↑
WIKI教程 @2018