本文實例講述了Spring Bean基本管理。分享給大家供大家參考,具體如下:
一、使用setter方式完成依賴注入
下面是Bean和beans-config.xml文件。
1
2
3
4
|
public class HelloBean { private String helloWord; //...省略getter、setter方法 } |
1
2
3
4
5
6
7
8
9
10
11
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> < beans > < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" > < property name = "helloWord" > < value >Hello!Justin!</ value > </ property > </ bean > </ beans > |
1
2
3
4
5
6
7
8
|
public class SpringDemo { public static void main(String[] args) { Resource rs = new FileSystemResource( "beans-config.xml" ); BeanFactory factory = new XmlBeanFactory(rs); HelloBean hello = (HelloBean) factory.getBean( "helloBean" ); System.out.println(hello.getHelloWord()); } } |
二、使用constructor方式完成注入
1
2
3
4
5
6
7
8
9
10
11
12
|
public class HelloBean { private String name; private String helloWord; // 建議有要無參數建構方法 public HelloBean() { } public HelloBean(String name, String helloWord) { this .name = name; this .helloWord = helloWord; } //...省略getter、setter方法 } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> < beans > < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" > < constructor-arg index = "0" > < value >Justin</ value > </ constructor-arg > < constructor-arg index = "1" > < value >Hello</ value > </ constructor-arg > </ bean > </ beans > |
1
2
3
4
5
6
7
8
9
10
11
|
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml" ); HelloBean hello = (HelloBean) context.getBean( "helloBean" ); System.out.print( "Name: " ); System.out.println(hello.getName()); System.out.print( "Word: " ); System.out.println(hello.getHelloWord()); } } |
三、屬性參考
1
2
3
4
5
|
public class HelloBean { private String helloWord; private Date date; //...省略getter、setter方法 } |
1
2
3
4
5
6
7
8
9
10
11
|
< beans > < bean id = "dateBean" class = "java.util.Date" /> < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" > < property name = "helloWord" > < value >Hello!</ value > </ property > < property name = "date" > < ref bean = "dateBean" /> </ property > </ bean > </ beans > |
1
2
3
4
5
6
7
8
9
10
11
|
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml" ); HelloBean hello = (HelloBean) context.getBean( "helloBean" ); System.out.print(hello.getHelloWord()); System.out.print( " It's " ); System.out.print(hello.getDate()); System.out.println( "." ); } } |
四、“byType”自動綁定
將“三”中的配置文件改為下面,即可完成bean屬性的按類型自動綁定。
1
2
3
4
5
6
7
8
|
< beans > < bean id = "dateBean" class = "java.util.Date" /> < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" autowire = "byType" > < property name = "helloWord" > < value >Hello!</ value > </ property > </ bean > </ beans > |
五、“byName”自動綁定
將“三”中的配置文件改為下面,即可完成bean屬性的按名稱自動綁定。
1
2
3
4
5
6
7
8
|
< beans > < bean id = "dateBean" class = "java.util.Date" /> < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" autowire = "byName" > < property name = "helloWord" > < value >Hello!</ value > </ property > </ bean > </ beans > |
六、“constructor”自動綁定
將“三”中的配置文件改為下面,即可完成bean屬性的按構造方法自動綁定。在建立依賴關系時,Srping容器會試圖比對容器中的Bean實例類型,及相關的構造方法上的參數類型,看看在類型上是否符合,如果有的話,則選用該構造方法來建立Bean實例。如果無法綁定,則拋出org.springframework.beans.factory.UnsatisfiedDependencyException異常。
1
2
3
4
5
6
7
8
|
< beans > < bean id = "dateBean" class = "java.util.Date" /> < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" autowire = "constructor" > < property name = "helloWord" > < value >Hello!</ value > </ property > </ bean > </ beans > |
六、“autodetect”自動綁定
將“三”中的配置文件改為下面,即可完成bean屬性的自動綁定,這個自動綁定是Spring會嘗試用入constructor來處理依賴關系的建立,如果不行,則再嘗試用byType類建立依賴關系。
1
2
3
4
5
6
7
8
|
< beans > < bean id = "dateBean" class = "java.util.Date" /> < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" autowire = "autodetect" > < property name = "helloWord" > < value >Hello!</ value > </ property > </ bean > </ beans > |
七、依賴檢查方式
在自動綁定中,由于沒辦法從定義文件中,清楚地看到是否每個屬性都完成設定,為了確定某些依賴關系確實建立,您可以假如依賴檢查,在<bean>標簽使用時設定"dependency-check",可以有四種依賴檢查方式:simple、objects、all、none。
simple:只檢查簡單的類型(像原生數據類型或字符串對象)屬性是否完成依賴關系,。
objects:檢查對象類型的屬性是否完成依賴關系。
all:則檢查全部的屬性是否完成依賴關系。
none:設定是默認值,表示不檢查依賴性。
1
2
3
4
5
6
7
8
|
< beans > < bean id = "dateBean" class = "java.util.Date" /> < bean id = "helloBean" class = "onlyfun.caterpillar.HelloBean" autowire = "autodetect" dependeny-check = "all" > < property name = "helloWord" > < value >Hello!</ value > </ property > </ bean > </ beans > |
八、集合對象注入
對于像數組、List、Set、Map等集合對象,在注入前必須填充一些對象至集合中,然后再將集合對象注入至所需的Bean時,也可以交由Spring的IoC容器來自動維護或生成集合對象,并完成依賴注入。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class SomeBean { private String[] someStrArray; private Some[] someObjArray; private List someList; private Map someMap; public String[] getSomeStrArray() { return someStrArray; } public void setSomeStrArray(String[] someStrArray) { this .someStrArray = someStrArray; } public Some[] getSomeObjArray() { return someObjArray; } public void setSomeObjArray(Some[] someObjArray) { this .someObjArray = someObjArray; } public List getSomeList() { return someList; } public void setSomeList(List someList) { this .someList = someList; } public Map getSomeMap() { return someMap; } public void setSomeMap(Map someMap) { this .someMap = someMap; } } public class Some { private String name; public String getName() { return name; } public void setName(String name) { this .name = name; } public String toString() { return name; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> < beans > < bean id = "some1" class = "onlyfun.caterpillar.Some" > < property name = "name" > < value >Justin</ value > </ property > </ bean > < bean id = "some2" class = "onlyfun.caterpillar.Some" > < property name = "name" > < value >momor</ value > </ property > </ bean > < bean id = "someBean" class = "onlyfun.caterpillar.SomeBean" > < property name = "someStrArray" > < list > < value >Hello</ value > < value >Welcome</ value > </ list > </ property > < property name = "someObjArray" > < list > < ref bean = "some1" /> < ref bean = "some2" /> </ list > </ property > < property name = "someList" > < list > < value >ListTest</ value > < ref bean = "some1" /> < ref bean = "some2" /> </ list > </ property > < property name = "someMap" > < map > < entry key = "MapTest" > < value >Hello!Justin!</ value > </ entry > < entry key = "someKey1" > < ref bean = "some1" /> </ entry > </ map > </ property > </ bean > </ beans > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml" ); SomeBean someBean = (SomeBean) context.getBean( "someBean" ); // 取得數組型態依賴注入對象 String[] strs = (String[]) someBean.getSomeStrArray(); Some[] somes = (Some[]) someBean.getSomeObjArray(); for ( int i = 0 ; i < strs.length; i++) { System.out.println(strs[i] + "," + somes[i].getName()); } // 取得List型態依賴注入對象 System.out.println(); List someList = (List) someBean.getSomeList(); for ( int i = 0 ; i < someList.size(); i++) { System.out.println(someList.get(i)); } // 取得Map型態依賴注入對象 System.out.println(); Map someMap = (Map) someBean.getSomeMap(); System.out.println(someMap.get( "MapTest" )); System.out.println(someMap.get( "someKey1" )); } } |
希望本文所述對大家Java程序設計有所幫助。