程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Spring bean 生命周期,springbean

Spring bean 生命周期,springbean

編輯:JAVA綜合教程

Spring bean 生命周期,springbean


Spring bean的生命周期是很容易理解。當一個bean實例化時,它可能需要執行一些初始化把它轉換成可用狀態。類似地,當bean不再需要,並且從容器中取出,一些清理的工作可能也需要做。

不過,還有把bean背後的實例化和銷毀時間之間的場景發生的活動,但是本章將只討論其中兩個是需要在bean的初始化和銷毀的時候,重要bean的生命周期回調方法。

要定義安裝和拆卸一個bean,我們只是聲明了初始化方法和/或銷毀,方法的參數<bean>。在init-method屬性指定一個方法,是被調用bean後立即實例化。同樣,銷毀方法規定了被調用當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中。這樣做可以確保正常關機,並讓所有的資源都被釋放調用singleton bean上的相關destroy方法。

建議不要使用的InitializingBean或者DisposableBean的回調,因為XML配置提供極大的靈活性在命名你的方法方面。

例如:

使用Eclipse IDE,然後按照下面的步驟來創建一個Spring應用程序:

步驟描述 1 Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project. 2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. 3 Create Java classes HelloWorld and MainApp under the com.manongjc package. 4 Create Beans configuration file Beans.xml under the src folder. 5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

這裡是HelloWorld.java的文件的內容:

package com.manongjc;

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 類中聲明一個關機hookregisterShutdownHook() 方法。這將確保正常關機,並調用相關的destroy方法。

package com.manongjc;

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.manongjc.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上。相反框架提供了靈活使用<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>

原文地址:http://www.manongjc.com/spring/spring_bean_life_cycle.html

相關閱讀:

  • Spring教程
  • Spring框架是什麼
  • Spring框架的體系結構
  • Spring 環境安裝配置
  • Spring 手把手入門實例
  • Spring IoC容器及原理
  • Spring bean定義
  • Spring Bean 的作用域
  • Spring bean 生命周期
  • Spring BeanPostProcessor 接口
  • Spring bean定義繼承
  • Spring 依賴注入
  • Spring注入內部bean
  • Spring注入集合
  • Spring Beans自動裝配
  • Spring事件處理
  • Spring基於注解配置
  • Spring 基於Java配置

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved