1. 程式人生 > 其它 >Spring例項化Bean的三種方法

Spring例項化Bean的三種方法

在面向物件的程式中,要想呼叫某個類的成員方法,就需要先例項化該類的物件。在 Spring 中,例項化 Bean 有三種方式,分別是構造器例項化、靜態工廠方式例項化和例項工廠方式例項化。本節將針對這三種方式分別進行講解。

構造器例項化

構造器例項化是指 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 所示。
在這裡插入圖片描述

例項工廠方式例項化

在 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進行例項化
        // 初始化Spring容器,載入配置檔案,並對bean進行例項化
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                xmlPath);
        // 通過容器獲取id為person3例項
        System.out.println(applicationContext.getBean("person3"));
    }
}