课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring Bean 生命周期

当前位置:免费教程 » Java相关 » Spring

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

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

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

初始化回调

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

  1. void afterPropertiesSet() throws Exception;

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

  1. public class ExampleBean implements InitializingBean {
  2. public void afterPropertiesSet() {
  3. // do some initialization work
  4. }
  5. }

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

  1. <bean id="exampleBean"
  2. class="examples.ExampleBean" init-method="init"/>

下面是类的定义:

  1. public class ExampleBean {
  2. public void init() {
  3. // do some initialization work
  4. }
  5. }

销毁回调

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

  1. void destroy() throws Exception;

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

  1. public class ExampleBean implements DisposableBean {
  2. public void destroy() {
  3. // do some destruction work
  4. }
  5. }

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

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

下面是类的定义:

  1. public class ExampleBean {
  2. public void destroy() {
  3. // do some destruction work
  4. }
  5. }

如果你在非 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 章节。
3com.tutorialspoint 包中创建 Java 类 HelloWorldMainApp
4src 文件夹中创建 Beans 配置文件 Beans.xml
5最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。

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

  1. package com.tutorialspoint;
  2. public class HelloWorld {
  3. private String message;
  4. public void setMessage(String message){
  5. this.message = message;
  6. }
  7. public void getMessage(){
  8. System.out.println("Your Message : " + message);
  9. }
  10. public void init(){
  11. System.out.println("Bean is going through init.");
  12. }
  13. public void destroy(){
  14. System.out.println("Bean will destroy now.");
  15. }
  16. }

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

  1. package com.tutorialspoint;
  2. import org.springframework.context.support.AbstractApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  7. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  8. obj.getMessage();
  9. context.registerShutdownHook();
  10. }
  11. }

下面是 init 和 destroy 方法必需的配置文件 Beans.xml 文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <bean id="helloWorld"
  7. class="com.tutorialspoint.HelloWorld"
  8. init-method="init" destroy-method="destroy">
  9. <property name="message" value="Hello World!"/>
  10. </bean>
  11. </beans>

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

  1. Bean is going through init.
  2. Your Message : Hello World!
  3. Bean will destroy now.

默认的初始化和销毁方法

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

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
  5. default-init-method="init"
  6. default-destroy-method="destroy">
  7. <bean id="..." class="...">
  8. <!-- collaborators and configuration for this bean go here -->
  9. </bean>
  10. </beans>
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号