1,概述
在Spring中提供了三種方式來對Bean進行配置:
在xml文件中配置在Java的接口和實現類中配置隱式Bean的發現機制和自動裝配原則
這三種方式都經常用到,而且常常會混合使用。這篇先寫xml裝配Bean。
2,分析bean標簽
1
2
3
4
5
|
<bean id= "pserson" class = "com.xl.spring.xlIoc.beans.Person" > <property name= "id" value= "1" /> <property name= "name" value= "宋智孝" /> <property name= "nickName" value= "懵懵" /> </bean> |
1.id:為bean取一個全局唯一的名字。該屬性可選,如果沒有聲明該屬性,那么Spring會采用"全限定名#{number}"的方式自動生成一個編號,number從0開始計數。
比如聲明了兩個Person對象,如下:
1
2
3
4
5
6
7
8
9
10
|
<bean class = "com.xl.spring.xlIoc.beans.Person" > <property name= "id" value= "1" /> <property name= "name" value= "宋智孝" /> <property name= "nickName" value= "懵懵" /> </bean> <bean class = "com.xl.spring.xlIoc.beans.Person" > <property name= "id" value= "2" /> <property name= "name" value= "周杰倫" /> <property name= "nickName" value= "Jack" /> </bean> |
這時想要獲取對象:
1
2
3
|
ApplicationContext ac = new ClassPathXmlApplicationContext( "springioc.xml" ); Person person = (Person) ac.getBean(Person. class .getName() + "#1" ); System.out.println(person.toString()); |
2.class:注入的對象的類型,對應的是類的全限定名。
3.property:定義類的屬性,其中name表示屬性名,value表示屬性值。
3,裝配集合
以上實現注入,都是對于一些基本數據類型和String類型。如果數據類型是集合的話,那么用如下做法:
1.首先定義一個新的類 --> Coder:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.xl.spring.xlIoc.beans; import lombok.Data; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @Data public class Coder { private String name; private List<String> languages; private Set<String> friends; private Map<String, Integer> houses; private Properties properties; } |
2.在Spring的xml文件中,注入Coder:
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
|
<bean id= "coder" class = "com.xl.spring.xlIoc.beans.Coder" > <property name= "name" value= "宋智孝" /> <!--配置list集合--> <property name= "languages" > <list> <value>java</value> <value>JavaScript</value> <value>SQL</value> </list> </property> <!--配置set集合--> <property name= "friends" > <set> <value>金鐘國</value> <value>河東勛</value> <value>劉在石</value> <value>池石鎮</value> <value>全邵旻</value> </set> </property> <!--配置map集合--> <property name= "houses" > <map> <entry key= "麻浦區" value= "240000000" /> </map> </property> <!--配置properties集合--> <property name= "properties" > <props> <prop key= "身高" > 168 </prop> <prop key= "體重" > 52.3 </prop> <prop key= "年齡" > 30 </prop> </props> </property> </bean> |
3.調用:
1
2
3
4
5
6
|
public static void testCoder() { //1.通過配置文件獲取到spring的上下文 ApplicationContext ac = new ClassPathXmlApplicationContext( "springioc.xml" ); Coder coder = (Coder) ac.getBean( "coder" ); coder.getFriends().forEach(s -> System.out.println( "friend ---> " + s)); } |
4,復雜Bean裝配
如果屬性是一個復雜集合對象,比如屬性是List或Map,而這個List的泛型是一個對象,或者說Map的key和value都是一個對象。
這個時候怎么辦?
解決思路:先把泛型對應的Bean注入好,然后在注入屬性的時候引入過去就好。
1.首先定義三個類:其中UserRole中需要使用User對象和Role對象
1
2
3
4
5
6
7
8
9
|
package com.xl.spring.xlIoc.beans; import lombok.Data; @Data public class User { private Integer id; private String 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
|
package com.xl.spring.xlIoc.beans; import lombok.Data; @Data public class Role { private Integer id; private String name; } package com.xl.spring.xlIoc.beans; import lombok.Data; import java.util.List; import java.util.Map; @Data public class UserRole { private Integer id; private List<User> users; private Map<Role, User> map; } |
2.先注入對應的User對象和Role對象,然后再注入UserRole對象:
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
|
<bean id= "user1" class = "com.xl.spring.xlIoc.beans.User" > <property name= "id" value= "1" /> <property name= "name" value= "張生" /> </bean> <bean id= "user2" class = "com.xl.spring.xlIoc.beans.User" > <property name= "id" value= "2" /> <property name= "name" value= "鶯鶯" /> </bean> <bean id= "role1" class = "com.xl.spring.xlIoc.beans.Role" > <property name= "id" value= "1" /> <property name= "name" value= "管理員" /> </bean> <bean id= "role2" class = "com.xl.spring.xlIoc.beans.Role" > <property name= "id" value= "2" /> <property name= "name" value= "普通用戶" /> </bean> <bean id= "userRole" class = "com.xl.spring.xlIoc.beans.UserRole" > <property name= "id" value= "1" /> <property name= "users" > <list> <ref bean= "user1" /> <ref bean= "user2" /> </list> </property> <property name= "map" > <map> <entry key-ref= "role1" value-ref= "user1" /> <entry key-ref= "role2" value-ref= "user2" /> </map> </property> </bean> |
3.創建對象并使用:
1
2
3
4
5
|
public static void testComplexBean() { ApplicationContext ac = new ClassPathXmlApplicationContext( "springioc.xml" ); UserRole userRole = (UserRole) ac.getBean( "userRole" ); System.out.println(userRole); } |
到此這篇關于Spring Xml裝配Bean的文章就介紹到這了,更多相關Spring Xml裝配Bean內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/qmcl-biu/p/15488361.html