前一篇文章中已经介绍了XML方式装配bean的方式,本文将综合这些方式举一个实例并进行测试,我会把所有类型的参数都放在同一个类中进行测试,下面是我的类结构:
上图是我画的一个基本结构,可以看出该类中有基本类型变量、引用变量、属性变量、集合变量且集合元素有基本类型和引用类型,我的引用类型中所有变量使用基本类型,所以先来创建两个引用类型CoderCard和CoderProject。
一、装配bean
1⃣️创建CoderCard.java
1 public class CoderCard { 2 private String id; 3 private String cardNotes; 4 private String coderId; 5 6 public String getId() { 7 return id; 8 } 9 10 public void setId(String id) { 11 this.id = id == null ? null : id.trim(); 12 } 13 14 public String getCardNotes() { 15 return cardNotes; 16 } 17 18 public void setCardNotes(String cardNotes) { 19 this.cardNotes = cardNotes == null ? null : cardNotes.trim(); 20 } 21 22 public String getCoderId() { 23 return coderId; 24 } 25 26 public void setCoderId(String coderId) { 27 this.coderId = coderId == null ? null : coderId.trim(); 28 } 29 }
2⃣️创建CoderProject.java
1 public class CoderProject { 2 private String id; 3 private String projectName; 4 private String projectVersion; 5 private String projectDesc; 6 7 public String getId() { 8 return id; 9 } 10 11 public void setId(String id) { 12 this.id = id == null ? null : id.trim(); 13 } 14 15 public String getProjectName() { 16 return projectName; 17 } 18 19 public void setProjectName(String projectName) { 20 this.projectName = projectName == null ? null : projectName.trim(); 21 } 22 23 public String getProjectVersion() { 24 return projectVersion; 25 } 26 27 public void setProjectVersion(String projectVersion) { 28 this.projectVersion = projectVersion == null ? null : projectVersion.trim(); 29 } 30 31 public String getProjectDesc() { 32 return projectDesc; 33 } 34 35 public void setProjectDesc(String projectDesc) { 36 this.projectDesc = projectDesc == null ? null : projectDesc.trim(); 37 } 38 }
3⃣️创建Coder.java
1 public class Coder { 2 private String id; 3 private String coderName; 4 private Integer coderAge; 5 private Integer coderSalary; 6 private CoderCard coderCard; 7 private Properties prop; 8 9 // 集合元素是基本类型 10 private List<String> coderBugList; 11 12 private Map<String, String> coderBugMap; 13 14 private Set<String> coderBugSet; 15 16 private String[] coderBugArray; 17 18 // 集合元素是引用类型 19 private List<CoderProject> coderProjectList; 20 21 private Map<CoderCard, CoderProject> coderProjectMap; 22 23 private Set<CoderProject> coderProjectSet; 24 25 private CoderProject[] coderProjectArray; 26 27 public String getId() { 28 return id; 29 } 30 31 public void setId(String id) { 32 this.id = id == null ? null : id.trim(); 33 } 34 35 public String getCoderName() { 36 return coderName; 37 } 38 39 public void setCoderName(String coderName) { 40 this.coderName = coderName == null ? null : coderName.trim(); 41 } 42 43 public Integer getCoderAge() { 44 return coderAge; 45 } 46 47 public void setCoderAge(Integer coderAge) { 48 this.coderAge = coderAge; 49 } 50 51 public Integer getCoderSalary() { 52 return coderSalary; 53 } 54 55 public void setCoderSalary(Integer coderSalary) { 56 this.coderSalary = coderSalary; 57 } 58 59 public CoderCard getCoderCard() { 60 return coderCard; 61 } 62 63 public void setCoderCard(CoderCard coderCard) { 64 this.coderCard = coderCard; 65 } 66 67 public List<String> getCoderBugList() { 68 return coderBugList; 69 } 70 71 public void setCoderBugList(List<String> coderBugList) { 72 this.coderBugList = coderBugList; 73 } 74 75 public Map<String, String> getCoderBugMap() { 76 return coderBugMap; 77 } 78 79 public void setCoderBugMap(Map<String, String> coderBugMap) { 80 this.coderBugMap = coderBugMap; 81 } 82 83 public Set<String> getCoderBugSet() { 84 return coderBugSet; 85 } 86 87 public void setCoderBugSet(Set<String> coderBugSet) { 88 this.coderBugSet = coderBugSet; 89 } 90 91 public Properties getProp() { 92 return prop; 93 } 94 95 public void setProp(Properties prop) { 96 this.prop = prop; 97 } 98 99 public String[] getCoderBugArray() { 100 return coderBugArray; 101 } 102 103 public void setCoderBugArray(String[] coderBugArray) { 104 this.coderBugArray = coderBugArray; 105 } 106 107 public List<CoderProject> getCoderProjectList() { 108 return coderProjectList; 109 } 110 111 public void setCoderProjectList(List<CoderProject> coderProjectList) { 112 this.coderProjectList = coderProjectList; 113 } 114 115 public Map<CoderCard, CoderProject> getCoderProjectMap() { 116 return coderProjectMap; 117 } 118 119 public void setCoderProjectMap(Map<CoderCard, CoderProject> coderProjectMap) { 120 this.coderProjectMap = coderProjectMap; 121 } 122 123 public Set<CoderProject> getCoderProjectSet() { 124 return coderProjectSet; 125 } 126 127 public void setCoderProjectSet(Set<CoderProject> coderProjectSet) { 128 this.coderProjectSet = coderProjectSet; 129 } 130 131 public CoderProject[] getCoderProjectArray() { 132 return coderProjectArray; 133 } 134 135 public void setCoderProjectArray(CoderProject[] coderProjectArray) { 136 this.coderProjectArray = coderProjectArray; 137 } 138 139 }
4⃣️XML中进行装配:spring-bean.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 8 <bean id="coder" class="com.hyc.pojo.Coder"> 9 <!-- 装配简易值 --> 10 <property name="id" value="1"></property> 11 <property name="coderName" value="张三"></property> 12 <property name="coderAge" value="26"></property> 13 <property name="coderSalary" value="10000"></property> 14 <!--装配对象 --> 15 <property name="coderCard" ref="coderCard"></property> 16 <!-- 装配Properties --> 17 <property name="prop"> 18 <props> 19 <prop key="bug-key-1">bug-prop-1</prop> 20 <prop key="bug-key-2">bug-prop-2</prop> 21 <prop key="bug-key-3">bug-prop-3</prop> 22 </props> 23 </property> 24 <!-- 装配集合,集合元素是基本类型 --> 25 <!-- 基本类型-装配List --> 26 <property name="coderBugList"> 27 <list> 28 <value>bug-list-1</value> 29 <value>bug-list-2</value> 30 <value>bug-list-3</value> 31 </list> 32 </property> 33 <!-- 基本类型-装配Map --> 34 <property name="coderBugMap"> 35 <map> 36 <entry key="bug1" value="bug-map-1" /> 37 <entry key="bug2" value="bug-map-2" /> 38 <entry key="bug3" value="bug-map-3" /> 39 </map> 40 </property> 41 <!-- 基本类型-装配Set --> 42 <property name="coderBugSet"> 43 <set> 44 <value>bug-set-1</value> 45 <value>bug-set-2</value> 46 <value>bug-set-3</value> 47 </set> 48 </property> 49 <!-- 基本类型-装配Array --> 50 <property name="coderBugArray"> 51 <array> 52 <value>bug-array-1</value> 53 <value>bug-array-2</value> 54 <value>bug-array-3</value> 55 </array> 56 </property> 57 58 <!-- 装配集合,集合元素是引用类型 --> 59 <!-- 引用类型-装配List --> 60 <property name="coderProjectList"> 61 <list> 62 <ref bean="coderProject1" /> 63 <ref bean="coderProject2" /> 64 </list> 65 </property> 66 <!-- 引用类型-装配Map --> 67 <property name="coderProjectMap"> 68 <map> 69 <entry key-ref="coderCard1" value-ref="coderProject1" /> 70 <entry key-ref="coderCard2" value-ref="coderProject2" /> 71 </map> 72 </property> 73 <!-- 引用类型-装配Set --> 74 <property name="coderProjectSet"> 75 <set> 76 <ref bean="coderProject1" /> 77 <ref bean="coderProject2" /> 78 </set> 79 </property> 80 <!-- 引用类型-装配Array --> 81 <property name="coderProjectArray"> 82 <array> 83 <ref bean="coderProject1" /> 84 <ref bean="coderProject2" /> 85 </array> 86 </property> 87 </bean> 88 89 <!-- 对象作为成员变量 --> 90 <bean id="coderCard" class="com.hyc.pojo.CoderCard"> 91 <property name="id" value="1"></property> 92 <property name="cardNotes" value="张三的工牌"></property> 93 <property name="coderId" value="1"></property> 94 </bean> 95 <!-- 对象作为成员变量 --> 96 <bean id="coderCard1" class="com.hyc.pojo.CoderCard"> 97 <property name="id" value="2"></property> 98 <property name="cardNotes" value="李四的工牌"></property> 99 <property name="coderId" value="2"></property> 100 </bean> 101 102 <bean id="coderCard2" class="com.hyc.pojo.CoderCard"> 103 <property name="id" value="3"></property> 104 <property name="cardNotes" value="王麻子的工牌"></property> 105 <property name="coderId" value="3"></property> 106 </bean> 107 108 <bean id="coderProject1" class="com.hyc.pojo.CoderProject"> 109 <property name="id" value="1"></property> 110 <property name="projectName" value="管理系统"></property> 111 <property name="projectVersion" value="V1.0"></property> 112 <property name="projectDesc" value="管理配置"></property> 113 </bean> 114 <bean id="coderProject2" class="com.hyc.pojo.CoderProject"> 115 <property name="id" value="2"></property> 116 <property name="projectName" value="存储系统"></property> 117 <property name="projectVersion" value="V1.0"></property> 118 <property name="projectDesc" value="存储配置"></property> 119 </bean> 120 121 </beans>
二、测试装配结果
1⃣️编写测试方法
因为我测试使用的是junit单元测试,所以先来编写一个单元测试积基类,完成ClassPathXmlApplicationContext类的实例化等操作;
第一步:创建单元测试基类
1 package com.test.xml; 2 3 import org.junit.After; 4 import org.junit.Before; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 /** 8 * Unit test for simple App. 9 */ 10 public class BaseTest { 11 12 public String xmlPath; 13 public ClassPathXmlApplicationContext context; 14 15 public BaseTest(String xmlPath) { 16 this.xmlPath = xmlPath; 17 } 18 19 @Before 20 public void before() { 21 if (xmlPath.isEmpty()) { 22 xmlPath = "classpath*:spring-*.xml"; 23 } 24 context = new ClassPathXmlApplicationContext(xmlPath.split("[,\s]")); 25 context.start(); 26 } 27 28 @SuppressWarnings("deprecation") 29 @After 30 public void after() { 31 context.destroy(); 32 } 33 34 }
第二步:创建bean装配的测试类,编写测试方法
1 package com.test.xml; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 8 import org.junit.Test; 9 10 import com.hyc.controller.CoderController; 11 import com.hyc.pojo.Coder; 12 import com.hyc.pojo.CoderCard; 13 import com.hyc.pojo.CoderProject; 14 15 /* 16 * 各种数据类型装载测试(XML方式) 17 */ 18 public class SpringBeanTest extends BaseTest { 19 public SpringBeanTest() { 20 super("classpath*:spring-bean.xml"); 21 } 22 23 /** 24 * 基本数据类型装配测试 25 */ 26 @Test 27 public void testBaseBean() { 28 Coder coder = (Coder) super.context.getBean("coder"); 29 String name = coder.getCoderName(); 30 System.out.println("name:" + name); 31 int age = coder.getCoderAge(); 32 System.out.println("age:" + age); 33 int salary = coder.getCoderSalary(); 34 System.out.println("salary:" + salary); 35 String cardNotes = coder.getCoderCard().getCardNotes(); 36 System.out.println("cardNotes:" + cardNotes); 37 Properties props = coder.getProp(); 38 System.out.println("props:" + props.getProperty("bug-key-1")); 39 } 40 41 /** 42 * 装配集合类型,集合元素是基本数据类型 43 */ 44 @Test 45 public void testBaseCollection() { 46 Coder coder = (Coder) super.context.getBean("coder"); 47 List<String> bugList = coder.getCoderBugList(); 48 System.out.println("bugList:" + bugList.size()); 49 Map<String, String> bugMap = coder.getCoderBugMap(); 50 System.out.println("bugMap:" + bugMap.size()); 51 String[] bugArray = coder.getCoderBugArray(); 52 System.out.println("bugArray:" + bugArray.length); 53 Set<String> bugSet = coder.getCoderBugSet(); 54 System.out.println("bugSet:" + bugSet.size()); 55 } 56 57 /** 58 * 装配集合类型,集合元素是引用类型 59 */ 60 @Test 61 public void testRefCollection() { 62 Coder coder = (Coder) super.context.getBean("coder"); 63 List<CoderProject> projectList = coder.getCoderProjectList(); 64 System.out.println("projectList:" + projectList.size()); 65 Map<CoderCard, CoderProject> projectMap = coder.getCoderProjectMap(); 66 System.out.println("projectMap:" + projectMap.size()); 67 CoderProject[] projectArray = coder.getCoderProjectArray(); 68 System.out.println("projectArray:" + projectArray.length); 69 Set<CoderProject> projectSet = coder.getCoderProjectSet(); 70 System.out.println("projectSet:" + projectSet.size()); 71 } 72 73 }
以上就是测试代码:
第一个方法是测试基本类型、引用类型和Properties类型;
第二个方法是测试集合类型,集合元素是基本类型;
第三个方法是测试集合类型,集合元素是引用类型;
测试时只要选中方法名,右击-Run As-Junit Test即可,如果要同时测试所有的方法,直接在该类中右击-Run As-Junit Test就行。测试集合的时候我只是输出了集合的size
2⃣️查看测试结果
以上就是通过XML装配bean的完整实例,其实这种方式是比较简单的,缺点是需要配置很多内容,为了解决这一缺陷,spring又提供了另外一种注解的方式,下一篇文章我将进行总结。