1. 程式人生 > 其它 >Spring通過容器建立物件的三種方式

Spring通過容器建立物件的三種方式

技術標籤:Springspring

Spring通過容器建立物件的三種方式

一、構造器例項化

構造器例項化是指 Spring 容器通過 Bean 對應的類中預設的建構函式例項化 Bean。下面通過案例演示如何使用構造器例項化 Bean。

1. 建立專案並匯入 JAR 包

在 MyEclipse 中建立一個名稱為 springDemo02 的 Web 專案,然後將 Spring 支援和依賴的 JAR 包複製到專案的 lib 目錄中,併發布到類路徑下。

2. 建立實體類

在專案的 src 目錄下建立一個名為 com.mengma.instance.constructor 的包,在該包下建立一個實體類 Person1,如下所示。

package com.mengma.instance.constructor;
public class Person1 {
    
}

3. 建立 Spring 配置檔案

在 com.mengma.instance.constructor 包下建立 Spring 的配置檔案 applicationContext.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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="person1" class
="com.mengma.instance.constructor.Person1"/>
</beans>

在上述配置中,定義了一個 id 為 person1 的 Bean,其中 class 屬性指定了其對應的類為 Person1。

4. 建立測試類

在 com.mengma.instance.constructor 包下建立一個名為 InstanceTest1 的測試類,編輯後如下所示。

package com.mengma.instance.constructor;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InstanceTest1 {    
    @Test    
    public void test() {        
        // 定義Spring配置檔案的路徑        
        String xmlPath = "com/mengma/instance/constructor/ApplicationContext.xml";     			// 初始化Spring容器,載入配置檔案,並對bean進行例項化        
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(                xmlPath);        
        // 通過容器獲取id為person1的例項  
        System.out.println(applicationContext.getBean("person1"));    
    }
}

上述檔案中,首先在 test() 方法中定義了 Spring 配置檔案的路徑,然後 Spring 容器會載入配置檔案。在載入的同時,Spring 容器會通過實現類 Person1 中預設的無參建構函式對 Bean 進行例項化。

5. 執行程式並檢視結果

使用 JUnit 測試執行 test() 方法,執行成功後,控制檯的輸出結果如圖 1 所示。

輸出結果
圖 1 輸出結果

從圖 1 的輸出結果中可以看出,Spring 容器已經成功對 Bean 進行了例項化,並輸出了結果。

注意:為了方便讀者的學習,本節中的所有配置檔案和 Java 檔案都根據知識點放置在同一個包中。在實際開發中,為了方便管理和維護,建議將這些檔案根據類別放置在不同目錄中。

二、靜態工廠方式例項化

在 Spring 中,也可以使用靜態工廠的方式例項化 Bean。此種方式需要提供一個靜態工廠方法建立 Bean 的例項。下面通過案例演示如何使用靜態工廠方式例項化 Bean。

1. 建立實體類

在專案的 src 目錄下建立一個名為 com.mengma.instance.static_factory 的包,並在該包下建立一個實體類 Person2,該類與 Person1 相同,不需要新增任何成員。

2. 建立靜態工廠類

在 com.mengma.instance.static_factory 包下建立一個名為 MyBeanFactory 的類,並在該類中建立一個名為 createBean() 的靜態方法,用於建立 Bean 的例項,如下所示。

package com.mengma.instance.static_factory;
public class MyBeanFactory {    
// 建立Bean例項的靜態工廠方法    
    public static Person2 createBean() {        
        return new Person2();    
    }
}

3. 建立 Spring 配置檔案

在 com.mengma.instance.static_factory 包下建立 Spring 的配置檔案 applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">    
    <bean id="person2" class="com.mengma.instance.static_factory.MyBeanFactory"        factory-method="createBean" />
</beans>

上述程式碼中,定義了一個 id 為 person2 的 Bean,其中 class 屬性指定了其對應的工廠實現類為 MyBeanFactory,而 factory-method 屬性用於告訴 Spring 容器呼叫工廠類中的 createBean() 方法獲取 Bean 的例項。

4. 建立測試類

在 com.mengma.instance.static_factory 包下建立一個名為 InstanceTest2 的測試類,編輯後如下所示。

package com.mengma.instance.static_factory;
import org.junit.Test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InstanceTest2 {    
    @Test    
    public void test() {        
        // 定義Spring配置檔案的路徑        
        String xmlPath = "com/mengma/instance/static_factory/applicationContext.xml"; 
        // 初始化Spring容器,載入配置檔案,並對bean進行例項化        
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(                xmlPath);        
        // 通過容器獲取id為person2例項        
        System.out.println(applicationContext.getBean("person2"));    
    }
}

5. 執行程式並檢視結果

使用 JUnit 測試執行 test() 方法,執行成功後,控制檯的輸出結果如圖 2 所示。

從圖 2 的輸出結果中可以看出,使用靜態工廠的方式也成功對 Bean 進行了例項化。

輸出結果
圖 2 輸出結果

三、例項工廠方式例項化

在 Spring 中,還有一種例項化 Bean 的方式就是採用例項工廠。在這種方式中,工廠類不再使用靜態方法建立 Bean 的例項,而是直接在成員方法中建立 Bean 的例項。

同時,在配置檔案中,需要例項化的 Bean 也不是通過 class 屬性直接指向其例項化的類,而是通過 factory-bean 屬性配置一個例項工廠,然後使用 factory-method 屬性確定使用工廠中的哪個方法。下面通過案例演示例項工廠方式的使用。

1. 建立實體類

在專案的 src 目錄下建立一個名為 com.mengma.instance.factory 的包,在該包下建立一個 Person3 類,該類與 Person1 類相同,不需要新增任何成員。

2. 建立例項工廠類

在 com.mengma.instance.factory 包下建立一個名為 MyBeanFactory 的類,編輯後如下所示。

package com.mengma.instance.factory;
public class MyBeanFactory {    
    public MyBeanFactory() {        
        System.out.println("person3工廠例項化中");    
    }    
    // 建立Bean的方法    
    public Person3 createBean() {        
        return new Person3();    
    }
}

上述程式碼中,使用預設無參的構造方法輸出 person3 工廠例項化中語句,使用 createBean 成員方法建立 Bean 的例項。

3. 建立 Spring 配置檔案

在 com.mengma.instance.factory 包下建立 Spring 的配置檔案 applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">    
    <!-- 配置例項工廠 -->    
    <bean id="myBeanFactory" class="com.mengma.instance.factory.MyBeanFactory" />    
    <!-- factory-bean屬性指定一個例項工廠,factory-method屬性確定使用工廠中的哪個方法 -->    	<bean id="person3" factory-bean="myBeanFactory" factory-method="createBean" />
</beans>

上述程式碼中,首先配置了一個例項工廠 Bean,然後配置了需要例項化的 Bean。在 id 為 person3 的 Bean 中,使用 factory-bean 屬性指定一個例項工廠,該屬性值就是例項工廠的 id 屬性值。使用 factory-method 屬性確定使用工廠中的 createBean() 方法。

4. 建立測試類

在 com.mengma.instance.factory 包下建立一個名為 InstanceTest3 的測試類,編輯後如下所示。

package com.mengma.instance.factory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InstanceTest3 {    
    @Test    
    public void test() {        
        // 定義Spring配置檔案的路徑       
        String xmlPath = "com/mengma/instance/factory/applicationContext.xml";      
        // 初始化Spring容器,載入配置檔案,並對bean進行例項化       
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(                xmlPath);        
        // 通過容器獲取id為person3例項        
        System.out.println(applicationContext.getBean("person3"));    
    }
}

5. 執行程式並檢視結果

使用 JUnit 測試執行 test() 方法,執行成功後,從輸出結果中可以看出,使用例項工廠的方式也同樣對 Bean 進行了例項化。