> 文章列表 > 【Spring】Bean的装配方式之基于XML配置的装配

【Spring】Bean的装配方式之基于XML配置的装配

【Spring】Bean的装配方式之基于XML配置的装配

1.创建Bean的实现类

在src目录中创建assemble包,在assemble包下创建ComplexUser类,在ComplexUser类

中分别使用构造方法注入和使用属性的setter方法注入。

package assemble;import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;public class ComplexUser {private String uname;private List<String> hobbyList;private Map<String,String> residenceMap;private Set<String> aliasSet;private String[] array;public ComplexUser(String unmae,List<String> hobbyList,Map<String,String> residenceMap,Set<String> aliasSet,String[] array){super();this.uname=uname;this.hobbyList=hobbyList;this.residenceMap=residenceMap;this.aliasSet=aliasSet;this.array=array;}public  ComplexUser(){super();}public void setUname(String uname) {this.uname = uname;}public void setHobbyList(List<String> hobbyList) {this.hobbyList = hobbyList;}public void setResidenceMap(Map<String, String> residenceMap) {this.residenceMap = residenceMap;}public void setAliasSet(Set<String> aliasSet) {this.aliasSet = aliasSet;}public void setArray(String[] array) {this.array = array;}@Overridepublic String toString() {return "ComplexUser{" +"uname='" + uname + '\\'' +", hobbyList=" + hobbyList +", residenceMap=" + residenceMap +", aliasSet=" + aliasSet +", array=" + Arrays.toString(array) +'}';}
}

2.配置Bean

在Spring配置文件中使用实现类ComplexUser配置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" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="user1" class="assemble.ComplexUser"><constructor-arg index="0" value="chenheng1"/><constructor-arg index="1"><list><value>唱歌</value><value>跳舞</value><value>打球</value></list></constructor-arg><constructor-arg index="2"><map><entry key="dalian" value="大连"></entry><entry key="beijing" value="北京"></entry><entry key="shanghai" value="上海"></entry></map></constructor-arg><constructor-arg index="3"><set><value>张三1</value><value>张三2</value><value>张三3</value></set></constructor-arg><constructor-arg index="4"><array><value>aaaaa</value><value>bbbbb</value></array></constructor-arg>
</bean><bean id="user2" class="assemble.ComplexUser"><property name="uname" value="chenheng2"/><property name="hobbyList"><list><value>看书</value><value>学习</value></list></property><property name="residenceMap"><map><entry key="shenzhen" value="深圳"></entry><entry key="guangzhou" value="广州"></entry><entry key="tianjin" value="天津"></entry></map></property><property name="aliasSet"><set><value>张三4</value><value>张三5</value><value>张三6</value></set></property><property name="array"><array><value>ccccc</value><value>ddddd</value></array></property></bean>
</beans>

3.测试基于XML配置的装配方式

bean文件名(applicationContent1.xml)

package assemble;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static  void main(String[] args) {ApplicationContext appCon =new ClassPathXmlApplicationContext("applicationContent1.xml");//构造方法ComplexUser u1= (ComplexUser) appCon.getBean("user1");System.out.println(u1);//set方法ComplexUser u2= (ComplexUser) appCon.getBean("user2");System.out.println(u2);}
}

4.运行结果