ponlya 发表于 2013-2-1 12:09:31

Spring3之 集合对象属性注入

com.spring305.test.beanInit.cpo.User.java
private int id;private String name;public User(){System.out.println(User.class);} setter ,getter方法略去。
com.spring305.test.beanInit.cpo.CollectionPo.java
import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;public class CollectionPo {private int id;private String name;private String[] testArray;private User[] useArray;private List testList;private Map testMap;private Set testSet;//对象private User user;//private Map<Integer, Float> maps ;public CollectionPo(){}public void test(){System.out.println(id+"_"+name);for (int i = 0; i < testArray.length; i++) {System.out.println("testArray "+testArray);}for (int i = 0; i < useArray.length; i++) {System.out.println("useArray "+((User)useArray).getName());}for (int i = 0; i < testList.size(); i++) {System.out.println("testList "+testList.get(i));}Iterator it = testMap.entrySet().iterator();//Iterator it2 = testMap.keySet().iterator();while (it.hasNext()) {System.out.println("testMap "+it.next());}Iterator it3 = testSet.iterator();//Iterator it2 = testMap.keySet().iterator();while (it3.hasNext()) {System.out.println("testSet "+it3.next());}System.out.println(user.getId()+"_"+user.getName() + " User");Iterator mapList = maps.entrySet().iterator();//Iterator it2 = testMap.keySet().iterator();while (mapList.hasNext()) {System.out.println("mapList "+mapList.next());}}。。。getter,setter方法略去} 
com.spring305.test.beanInit.TestCollectionBeanInit.java
@Testpublic void InitSingeBean() {ApplicationContext context =new ClassPathXmlApplicationContext("testCollectionBeanInit.xml");CollectionPo collectionPo = (CollectionPo) context.getBean("collectionPo");collectionPo.test();} src/testCollectionBeanInit.xml
<bean id="userBean" class="com.spring305.test.beanInit.cpo.User"> <property name="id" value="123"></property> <property name="name" value="名字"></property></bean><!--  <value/>这样就可以为string类型的设置null值 <null/>也行 --><bean id="collectionPo" class="com.spring305.test.beanInit.cpo.CollectionPo"> <property name="id"><value>123</value></property> <property name="name"><null/></property> <property name="testArray">   <list>     <value>Hello!1!</value>             <value>Hello!2!</value>             <value>Hello!3!</value>   </list> </property> <property name="useArray">   <list>     <ref bean="userBean"/>     <ref bean="userBean"/>     <ref bean="userBean"/>   </list> </property> <property name="testList">   <list>     <value>Hello!1!</value>     <ref bean="userBean"/>     <ref bean="userBean"/>   </list> </property> <property name="testMap">  <map>  <!-- <entry key ="a ref" value-ref="userBean"/> -->   <entry key="somekey1">                    <ref bean="userBean"/>                </entry>   <entry key="somekey2">                    <value>Hello!1!</value>                </entry>  </map> </property> <property name="testSet">  <map>   <entry key="somekey1">                    <ref bean="userBean"/>                </entry>   <entry key="somekey2">                    <value>Hello!1!</value>                </entry>                <!-- <entry key="somekey3" value="hello"/> -->  </map> </property> <property name="user">  <bean class="com.spring305.test.beanInit.cpo.User">   <property name="id" value="123"></property>  <property name="name"><null/></property>  </bean> </property> <property name="maps">   <props>         <prop key="12">12.3</prop>         <prop key="34">31.2</prop>         <prop key="1">31.2</prop>     </props>     <!--           <property name="maps">           <map>               <entry key="1" value="9.99"/>               <entry key="2" value="2.75"/>               <entry key="3" value="3.99"/>           </map>       </property>      --> </property></bean><!--xmlns:p="http://www.springframework.org/schema/p" p名称空间没有标准的XML格式定义灵活 集合可以通过 parent="XX"  props merge="true" 来对集合合并  --><!--  <property name="user">   <bean class="com.spring305.test.beanInit.cpo.User">    <property name="id" value="123"></property>    <property name="name" value="名字"></property>   </bean>  </property> --> <!-- spring3文档中其它配置  'c-namespace' declaration <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com">  --> <!-- 'c-namespace' index declaration <bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz"> --> 
页: [1]
查看完整版本: Spring3之 集合对象属性注入