在面向對象的程序中,要想調用某個類的成員方法,就需要先實例化該類的對象。在 Spring 中,實例化 Bean 有三種方式,分別是構造器實例化、靜態工廠方式實例化和實例工廠方式實例化。
構造器實例化
構造器實例化是指 Spring 容器通過 Bean 對應的類中默認的構造函數實例化 Bean。下面通過案例演示如何使用構造器實例化 Bean。
1. 創建項目并導入 JAR 包 在 MyEclipse 中創建一個名稱為 spring 的 Web 項目,然后將 Spring 支持和依賴的 JAR 包復制到項目的 lib 目錄中,并發布到類路徑下。
2. 創建實體類 在項目的 src 目錄下創建一個名為 com.mengma.instance.constructor 的包,在該包下創建一個實體類 Person,如下所示。
package com
. mengma
. instance
. constructor
;
public class Person {
}
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
/ beanshttp
: / / www
. springframework
. org
/ schema
/ beans
/ spring
- beans
- 3.2 . xsd"
> < bean id
= "person" class = "com.mengma.instance.constructor.Person" / >
< / beans
>
在上述配置中,定義了一個 id 為 person 的 Bean,其中 class 屬性指定了其對應的類為 Person。
4. 創建測試類 在 com.mengma.instance.constructor 包下創建一個名為 InstanceTest 的測試類,編輯后如下所示。
package com
. mengma
. instance
. constructor
; import org
. junit
. Test
;
import org
. springframework
. context
. ApplicationContext
;
import org
. springframework
. context
. support
. ClassPathXmlApplicationContext
; public class InstanceTest { @Test public void test ( ) { String xmlPath
= "com/mengma/instance/constructor/ApplicationContext.xml" ; ApplicationContext applicationContext
= new ClassPathXmlApplicationContext ( xmlPath
) ; System
. out
. println ( applicationContext
. getBean ( "person" ) ) ; }
}
上述文件中,首先在 test() 方法中定義了 Spring 配置文件的路徑,然后 Spring 容器會加載配置文件。在加載的同時,Spring 容器會通過實現類 Person1 中默認的無參構造函數對 Bean 進行實例化。
5. 運行程序并查看結果 使用 JUnit 測試運行 test() 方法,運行成功后,控制臺的輸出結果如下圖所示。
從輸出結果中可以看出,Spring 容器已經成功對 Bean 進行了實例化,并輸出了結果。
靜態工廠方式實例化
在 Spring 中,也可以使用靜態工廠的方式實例化 Bean。此種方式需要提供一個靜態工廠方法創建 Bean 的實例。下面通過案例演示如何使用靜態工廠方式實例化 Bean。
1. 創建實體類 在項目的 src 目錄下創建一個名為 com.mengma.instance.static_factory 的包,并在該包下創建一個實體類 Person,不需要添加任何成員。
2. 創建靜態工廠類 在 com.mengma.instance.static_factory 包下創建一個名為 MyBeanFactory 的類,并在該類中創建一個名為 createBean() 的靜態方法,用于創建 Bean 的實例,如下所示。
package com
. mengma
. instance
. static_factory
; public class MyBeanFactory { public static Person
createBean ( ) { return new Person ( ) ; }
}
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
/ beanshttp
: / / www
. springframework
. org
/ schema
/ beans
/ spring
- beans
- 3.2 . xsd"
> < bean id
= "person" class = "com.mengma.instance.static_factory.MyBeanFactory" factory
- method
= "createBean" / >
< / beans
>
上述代碼中,定義了一個 id 為 person 的 Bean,其中 class 屬性指定了其對應的工廠實現類為 MyBeanFactory,而 factory-method 屬性用于告訴 Spring 容器調用工廠類中的 createBean() 方法獲取 Bean 的實例。
4. 創建測試類 在 com.mengma.instance.static_factory 包下創建一個名為 InstanceTest 的測試類,編輯后如下所示。
package com
. mengma
. instance
. static_factory
; import org
. junit
. Test
;
import org
. springframework
. context
. ApplicationContext
;
import org
. springframework
. context
. support
. ClassPathXmlApplicationContext
; public class InstanceTest { @Test public void test ( ) { String xmlPath
= "com/mengma/instance/static_factory/applicationContext.xml" ; ApplicationContext applicationContext
= new ClassPathXmlApplicationContext ( xmlPath
) ; System
. out
. println ( applicationContext
. getBean ( "person" ) ) ; }
}
5. 運行程序并查看結果 使用 JUnit 測試運行 test() 方法,運行成功后,控制臺的輸出結果如下圖所示。 從輸出結果中可以看出,使用靜態工廠的方式也成功對 Bean 進行了實例化。
實例工廠方式實例化
在 Spring 中,還有一種實例化 Bean 的方式就是采用實例工廠。在這種方式中,工廠類不再使用靜態方法創建 Bean 的實例,而是直接在成員方法中創建 Bean 的實例。
同時,在配置文件中,需要實例化的 Bean 也不是通過 class 屬性直接指向其實例化的類,而是通過 factory-bean 屬性配置一個實例工廠,然后使用 factory-method 屬性確定使用工廠中的哪個方法。下面通過案例演示實例工廠方式的使用。
1. 創建實體類 在項目的 src 目錄下創建一個名為 com.mengma.instance.factory 的包,在該包下創建一個 Person 類,不需要添加任何成員。
2. 創建實例工廠類 在 com.mengma.instance.factory 包下創建一個名為 MyBeanFactory 的類,編輯后如下所示。
package com
. mengma
. instance
. factory
; public class MyBeanFactory { public MyBeanFactory ( ) { System
. out
. println ( "person工廠實例化中" ) ; } public Person
createBean ( ) { return new Person ( ) ; }
}
上述代碼中,使用默認無參的構造方法輸出 person 工廠實例化中語句,使用 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
/ beanshttp
: / / 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
= "person" factory
- bean
= "myBeanFactory" factory
- method
= "createBean" / >
< / beans
>
上述代碼中,首先配置了一個實例工廠 Bean,然后配置了需要實例化的 Bean。在 id 為 person 的 Bean 中,使用 factory-bean 屬性指定一個實例工廠,該屬性值就是實例工廠的 id 屬性值。使用 factory-method 屬性確定使用工廠中的 createBean() 方法。
4. 創建測試類 在 com.mengma.instance.factory 包下創建一個名為 InstanceTest 的測試類,編輯后如下所示。
package com
. mengma
. instance
. factory
; import org
. junit
. Test
;
import org
. springframework
. context
. ApplicationContext
;
import org
. springframework
. context
. support
. ClassPathXmlApplicationContext
; public class InstanceTest { @Test public void test ( ) { String xmlPath
= "com/mengma/instance/factory/applicationContext.xml" ; ApplicationContext applicationContext
= new ClassPathXmlApplicationContext ( xmlPath
) ; System
. out
. println ( applicationContext
. getBean ( "person" ) ) ; }
}
5. 運行程序并查看結果 使用 JUnit 測試運行 test() 方法,運行成功后,控制臺的輸出結果如下圖所示。
從輸出結果中可以看出,使用實例工廠的方式也同樣對 Bean 進行了實例化。
總結
以上是生活随笔 為你收集整理的Spring实例化Bean 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。