生活随笔
收集整理的這篇文章主要介紹了
Spring基于XML装配Bean
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Bean 的裝配可以理解為依賴關系注入,Bean 的裝配方式也就是 Bean 的依賴注入方式。Spring 容器支持多種形式的 Bean 的裝配方式,如基于 XML 的 Bean 裝配、基于 Annotation 的 Bean 裝配和自動裝配等。
Spring 基于 XML 的裝配通常采用兩種實現方式,即設值注入(Setter Injection)和構造注入(Constructor Injection)。
在 Spring 實例化 Bean 的過程中,首先會調用默認的構造方法實例化 Bean 對象,然后通過 Java 的反射機制調用 setXxx() 方法進行屬性的注入。因此,設值注入要求一個 Bean 的對應類必須滿足以下兩點要求。
必須提供一個默認的無參構造方法。必須為需要注入的屬性提供對應的 setter 方法。
使用設值注入時,在 Spring 配置文件中,需要使用 元素的子元素 元素為每個屬性注入值。而使用構造注入時,在配置文件中,主要使用 標簽定義構造方法的參數,可以使用其 value 屬性(或子元素)設置該參數的值。 示例基于 XML 方式的 Bean 的裝配。 1. 創建 Person 類 在項目 spring中的 src 目錄下,創建一個名稱為 com.zsh 的包,在該包下創建一個 Person 類,如下所示。
package com
. zsh
; public class Person { private String name
; private int age
; public String
getName ( ) { return name
; } public void setName ( String name
) { this . name
= name
; } public int getAge ( ) { return age
; } public void setAge ( int age
) { this . age
= age
; } public String
toString ( ) { return "Person[name=" + name
+ ",age=" + age
+ "]" ; } public Person ( ) { super ( ) ; } public Person ( String name
, int age
) { super ( ) ; this . name
= name
; this . age
= age
; }
}
上述代碼中,定義了 name 和 age 兩個屬性,并為其提供了 getter 和 setter 方法,由于要使用構造注入,所以需要提供有參的構造方法。為了能更清楚地看到輸出結果,這里還重寫了 toString() 方法。
2. 創建 Spring 配置文件 在 com.zsh 包下創建一個名為 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"
> < ! -- 使用設值注入方式裝配Person實例
-- > < bean id
= "person1" class = "zsh.Person" > < property name
= "name" value
= "zhangsan" / > < property name
= "age" value
= "20" / > < / bean
> < ! -- 使用構造方法裝配Person實例
-- > < bean id
= "person2" class = "com.zsh.Person" > < constructor
- arg index
= "0" value
= "lisi" / > < constructor
- arg index
= "1" value
= "21" / > < / bean
>
< / beans
>
上述代碼中,首先使用了設值注入方式裝配 Person 類的實例,其中 子元素用于調用 Bean 實例中的 setXxx() 方法完成屬性賦值。然后使用了構造方式裝配了 Person 類的實例,其中 元素用于定義構造方法的參數,其屬性 index 表示其索引(從 0 開始),value 屬性用于設置注入的值。
3. 創建測試類 在 com.zsh包下創建一個名稱為 XmlBeanAssemblyTest 的測試類,編輯后如下所示。
package com
. zsh
; import org
. junit
. Test
;
import org
. springframework
. context
. ApplicationContext
;
import org
. springframework
. context
. support
. ClassPathXmlApplicationContext
; public class XmlBeanAssemblyTest { @Test public void test ( ) { String xmlPath
= "com/zsh/applicationContext.xml" ; ApplicationContext applicationContext
= new ClassPathXmlApplicationContext ( xmlPath
) ; System
. out
. println ( applicationContext
. getBean ( "person1" ) ) ; System
. out
. println ( applicationContext
. getBean ( "person2" ) ) ; }
}
上述代碼中,分別獲取并輸出了 id 為 person1 和 person2 的實例。 4. 運行項目并查看結果 使用 JUnit 測試運行 test() 方法,運行成功后,控制臺的輸出結果如下圖所示。
從輸出結果中可以看出,使用設值注入和構造注入兩種方式都成功裝配了 Person 實例。
總結
以上是生活随笔 為你收集整理的Spring基于XML装配Bean 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。