Spring - Bean 的定義

這裏給出Bean的定義,還是英文的比較好理解,就不翻譯了,怎麼翻譯怎麼不對。

The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.

Spring Ioc容器管理Bean的實例化、裝配以及其他行爲。Bean是容器由相對應的配置生成的,這樣的配置可以是XML,正如前面看到的。

Bean的定義

Bean的定義需要的信息叫做configuration metadata (配置元數據),這些信息告訴容器以下幾點:
- 如何創建Bean
- Bean的生命週期
- Bean的依賴

配置元數據

Configuration metadata 包含一系列屬性:

No name Properties & Description
1 class 該屬性是強制性的,用來指定用來創建Bean的Bean類
2 name 該屬性指定了Bean的唯一標識符。在XML配置中,你可以使用id 或者 name 屬性來指定bean的唯一標識符
3 scope 指定從一個特定的bean定義創建的對象的範圍,在後續討論
4 constructor-arg 通過構造函數注入的依賴,後續討論
5 autowiring mode 用來注入依賴關係的,後續討論
6 lazy-initialization mode 延遲初始化,如果設置true,表示當該bean第一次被請求的時候纔會被創建,而不是在容器啓動的時候
7 initialization method 回調-在bean的所有必須的屬性被容器設置後會調用該函數
8 destruction method 當包含該bean的容器被銷燬的時候調用該毀掉方法

Spring Configuration Metadata

IoC 容器與配置元數據實際寫入的格式是完全分離的。以下三種方法提供給Spring 容器的配置元數據
- XML 文件配置
- 基於註解的配置
- Java config的配置

你已經看過基於XML的配置,讓我們看看其他基於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">

   <!-- A simple bean definition -->
   <bean id = "..." class = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with lazy init set on -->
   <bean id = "..." class = "..." lazy-init = "true">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with initialization method -->
   <bean id = "..." class = "..." init-method = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with destruction method -->
   <bean id = "..." class = "..." destroy-method = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- more bean definitions go here -->

</beans>

遇到的問題

在寫到init-method和destroy-method時,發現怎麼設置destroy-method,最後的銷燬方法都沒有被執行。

開始我的MainApp的方法是這樣的:

package com.soygrow;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
        helloWorld.getMessage();
        helloWorld.setMessage("singleton test.");

        HelloWorld helloWorld1 = (HelloWorld) context.getBean("helloWorld");
        helloWorld1.getMessage();
    }
}

這樣寫表示Application 沒有結束,所以導致destroy-method怎麼也不會執行,所以如果想執行你配置的destroy-method方法需要主動進行結束 context。我嘗試的方法是:

((ClassPathXmlApplicationContext) context).close();

如果你將ApplicationContext替換成AbstractApplicationContext,那麼context也是有close的方法的。

總之,需要將context 關閉纔會調用destroy-method的方法。

Spring教程專欄地址:http://blog.csdn.net/column/details/19452.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章