 
- Spring 核心基础知识
- 春天 - 主页
- 春天 - 概述
- 春天-建筑
- Spring - 环境设置
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定义
- Spring - Bean 范围
- Spring - Bean 生命周期
- Spring - Bean 后处理器
- Spring-Bean定义继承
- Spring - 依赖注入
- Spring - 注入内部 Bean
- Spring - 注入集合
- Spring - Bean 自动装配
- 基于注释的配置
- Spring - 基于Java的配置
- Spring - Spring 中的事件处理
- Spring - Spring 中的自定义事件
- Spring - 使用 Spring 框架的 AOP
- Spring - JDBC 框架
- Spring-事务管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 进行日志记录
- 春季问答
- 春天 - 问题与解答
- 春季有用资源
- 春天 - 快速指南
- Spring - 有用的资源
- 春天 - 讨论
Spring - Bean 生命周期
Spring 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 上调用相关的销毁方法,以便释放所有资源。
建议您不要使用 InitializingBean 或 DisposableBean 回调,因为 XML 配置在命名方法方面提供了很大的灵活性。
例子
让我们准备好一个可用的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 -
| 脚步 | 描述 | 
|---|---|
| 1 | 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.tutorialspoint 。 | 
| 2 | 使用“添加外部 JAR”选项添加所需的 Spring 库,如Spring Hello World 示例一章中所述。 | 
| 3 | 在com.tutorialspoint包下创建 Java 类HelloWorld和MainApp。 | 
| 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 类上声明。这将确保正常关闭并调用相关的销毁方法。
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-method和destroy-method 。相反,框架提供了使用<beans> 元素上的default-init-method和default-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>