05_集合对象注入
【工程截图】
【Some.java】
package com.HigginCui; public class Some { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "some [name=" + name + "]"; } }
【SomeBean.java】
package com.HigginCui; import java.util.List; import java.util.Map; public class SomeBean { private String[] someStrArray; //字符串数组 private Some[] someObjArray; //类数组 private List someList; //List集合 private Map someMap; //Map集合 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; } }
【applicationContext.xml】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="some1" class="com.HigginCui.Some"> <property name="name"> <value>HigginCui</value> </property> </bean> <bean id="some2" class="com.HigginCui.Some"> <property name="name"> <value>Jack</value> </property> </bean> <!-- 1.对于数组或List类型的依赖注入,在编写定义文件时,用<list>标签,并使用<value>标签指定字符串 或是使用<ref>标签来参考至其它的bean实例 2.对于Map类型的依赖关系注入则是使用<map>标签,map必须指定key-value 所以要用<entry>标签来指定<key>,然后使用<value>标签指定字符串 或是使用<ref>来参考至其它的bean实例 --> <bean id="someBean" class="com.HigginCui.SomeBean"> <!-- 字符串数组: String[] someStrArray;--> <property name="someStrArray"> <list> <value>Hello</value> <value>world...</value> </list> </property> <!-- Some类数组:Some[] someObjArray; --> <property name="someObjArray"> <list> <ref bean="some1"/> <ref bean="some2"/> </list> </property> <!-- List集合:List someList; --> <property name="someList"> <list> <value>ListTest</value> <ref bean="some1"/> <ref bean="some2"/> </list> </property> <!-- Map集合:Map someMap; --> <property name="someMap"> <map> <entry key="MapTest"> <value>Hello HigginCui!</value> </entry> <entry key="someKey1"> <ref bean="some1"/> </entry> </map> </property> </bean> </beans>
【TestAll.java】
package com.HigginCui.test; import java.util.List; import java.util.Map; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.HigginCui.Some; import com.HigginCui.SomeBean; public class TestAll { @Test public void testSomeBean(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); SomeBean someBean=(SomeBean) context.getBean("someBean"); //取得字符串数组类型依赖注入对象 String[] strs=someBean.getSomeStrArray(); for(int i=0;i<strs.length;i++){ System.out.println("strs["+i+"]= "+strs[i]); } //取得Some类数组类型依赖注入对象 Some[] somes=someBean.getSomeObjArray(); for(int i=0;i<somes.length;i++){ System.out.println("some["+i+"].name="+somes[i].getName()); } //取得List类型依赖注入对象 List someList=someBean.getSomeList(); for(int i=0;i<someList.size();i++){ System.out.println("someList.get("+i+")="+someList.get(i)); } //取得Map类型依赖注入对象 Map someMap=someBean.getSomeMap(); System.out.println(someMap.get("MapTest")); System.out.println(someMap.get("someKey1")); } }
【运行结果】
strs[0]= Hello strs[1]= world... some[0].name=HigginCui some[1].name=Jack someList.get(0)=ListTest someList.get(1)=some [name=HigginCui] someList.get(2)=some [name=Jack] Hello HigginCui! some [name=HigginCui]