使用XML裝配Bean需要定義對於的XML,需要引入對應的XML模式(XSD)文件,這些文件會定義配置Spring Bean的一些元素,簡單的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
創建角色類Role
類構造器有參與無參
代碼:
Role:

package com.wbg.springxmlbean.entity; public class Role { private int id; private String roleName; private String note; @Override public String toString() { return "Role{" + "id=" + id + ", roleName='" + roleName + '\'' + ", note='" + note + '\'' + '}'; } public Role() { } public Role(int id, String roleName, String note) { this.id = id; this.roleName = roleName; this.note = note; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } }
User:

package com.wbg.springxmlbean.entity; public class User { private int id; private Role role; @Override public String toString() { return "User{" + "id=" + id + ", role=" + role + ", name='" + name + '\'' + ", age=" + age + '}'; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } private String name; private int age; }
xml進行配置:
1、裝備簡易值
<!-- id:屬性是Spring找到的這個Bean的編號,不是必須的,如果沒有Spring會采用: "全限定名#{number}"的格式生成編號 列如: <bean class="com.wbg.springxmlbean.entity.Role"> Spring會生成編號為:"com.wbg.springxmlbean.entity.Role#1" class:是一個類的全限定名 --> <bean id="role1" class="com.wbg.springxmlbean.entity.Role"> <!-- property元素是定義類的屬性,name屬性定義的是屬性名稱 value是值 相當於: Role role=new Role(); role.setId(1); role.setRoleName("高級工程師"); role.setNote("重要人員");--> <property name="id" value="1"/> <property name="roleName" value="高級工程師"/> <property name="note" value="重要人員"/> </bean> <bean id="rolew" class="com.wbg.springxmlbean.entity.Role"> <!-- constructor-arg元素,index代表參數索引, value是值 相當於: Role role=new Role(1,"高級工程師","重要人員");--> <constructor-arg index="0" value="1"/> <constructor-arg index="1" value="高級工程師"/> <constructor-arg index="2" value="重要人員"/> </bean>
<bean id="user" class="com.wbg.springxmlbean.entity.User"> <property name="id" value="1"/> <property name="age" value="18"/> <property name="name" value="韋邦杠"/> <!--name是屬性名稱 ref是對應的Bean--> <property name="role" ref="role1"/> </bean>
測試:
public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Role.xml"); UserService userService= (UserService) applicationContext.getBean("userService"); userService.setUser((User)applicationContext.getBean("user")); System.out.println(userService.getUser()); }
2、裝配集合
定義類:

package com.wbg.springxmlbean.entity; import java.util.*; public class ComplexAssembly { private Long id; private List<String> list; private Map<String,String> map; private Properties properties; private Set<String> set; private String[] array; @Override public String toString() { return "ComplexAssembly{" + "id=" + id + ", list=" + list + ", map=" + map + ", properties=" + properties + ", set=" + set + ", array=" + Arrays.toString(array) + '}'; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } }
xml:
<bean id="complexAssembly" class="com.wbg.springxmlbean.entity.ComplexAssembly"> <property name="id" value="1"/> <property name="list"> <!--List屬性對應list元素進行裝配,然后通過多個value設值--> <list> <value>value-list-1</value> <value>value-list-2</value> <value>value-list-3</value> <value>value-list-4</value> </list> </property> <property name="map"> <!--Map屬性對應map元素進行裝配,然后通過多個entry設值,只是entry包含有key和value值設值--> <map> <entry key="key1" value="value1"/> <entry key="key2" value="value2"/> <entry key="key3" value="value3"/> <entry key="key4" value="value4"/> </map> </property> <property name="properties"> <!--Properties屬性,對應props進行裝配,然后通過prop元素數值,只是prop有一個必填的key,然后設值--> <props> <prop key="prop1">value-prop-1</prop> <prop key="prop2">value-prop-2</prop> <prop key="prop3">value-prop-3</prop> <prop key="prop4">value-prop-4</prop> </props> </property> <property name="set"> <!--Set屬性對應set元素進行裝配,然后通過多個value設值--> <set> <value>value-set-1</value> <value>value-set-2</value> <value>value-set-3</value> <value>value-set-4</value> </set> </property> <property name="array"> <!--Array屬性對應array元素進行裝配,然后通過多個value設值--> <array> <value>value-array-1</value> <value>value-array-2</value> <value>value-array-3</value> <value>value-array-4</value> </array> </property> </bean>
測試:
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Role.xml"); ComplexAssembly complexAssembly= (ComplexAssembly) applicationContext.getBean("complexAssembly"); System.out.println(complexAssembly);
3、裝配用戶和角色
類:MapUserRole

package com.wbg.springxmlbean.entity; import java.util.Map; public class MapUserRole { private Map<User,Role> map; @Override public String toString() { return "MapUserRole{" + "map=" + map + '}'; } public Map<User, Role> getMap() { return map; } public void setMap(Map<User, Role> map) { this.map = map; } }
xml:

<bean id="u2" class="com.wbg.springxmlbean.entity.User"> <property name="id" value="1"/> <property name="name" value="小邦哥"/> <property name="age" value="20"/> </bean> <bean id="u1" class="com.wbg.springxmlbean.entity.User"> <property name="id" value="2"/> <property name="name" value="邦杠"/> <property name="age" value="21"/> </bean> <bean id="r1" class="com.wbg.springxmlbean.entity.Role"> <constructor-arg index="0" value="1"/> <constructor-arg index="1" value="中級工程師"/> <constructor-arg index="2" value="普通人員"/> </bean> <bean id="r2" class="com.wbg.springxmlbean.entity.Role"> <constructor-arg index="0" value="2"/> <constructor-arg index="1" value="高級工程師"/> <constructor-arg index="2" value="重要人員"/> </bean> <bean id="mapUserRole" class="com.wbg.springxmlbean.entity.MapUserRole"> <property name="map"> <map> <entry key-ref="u1" value-ref="r1"/> <entry key-ref="u2" value-ref="r2"/> </map> </property> </bean>
測試: