0
点赞
收藏
分享

微信扫一扫

集合类型的装配


知识点:

【 


第一:设置集合属性 

public class OrderServiceBean { 

 private Set<String> sets = new HashSet<String>(); 

 private List<String> lists = new ArrayList<String>(); 

 private Properties properties = new Properties(); 

 private Map<String, String> maps = new HashMap<String, String>(); 

 ....//这里省略属性的getter和setter方法 

} 


第二:为是属性设置值 


<bean id="order" class="cn.itcast.service.OrderServiceBean"> 

 <property name="lists"> 

 <list> 

 <value>lihuoming</value> 

 </list> 

 </property> 

 <property name="sets"> 

 <set> 

 <value>set</value> 

 </set> 

 </property> 

 <property name="maps"> 

 <map> 

 <entry key="lihuoming" value="28"/> 

 </map> 

 </property> 

 <property name="properties"> 

 <props> 

 <prop key="12">sss</prop> 

 </props> 

 </property> 

</bean> 


】 


搭建环境见上... 


照样实现步骤: 


第一步:编写Bean 



public class PersonServer implements IPersonServer { 


 //集合类型的装配 


 private Set<String> sets = new HashSet<String>(); 

 private List<String> lists = new ArrayList<String>(); 

 private Properties properties = new Properties(); 

 private Map<String, String> maps = new HashMap<String, String>(); 


 //生成getter和setter方法 

 public Set<String> getSets() { 

 return sets; 

 } 

 public void setSets(Set<String> sets) { 

 this.sets = sets; 

 } 

 public List<String> getLists() { 

 return lists; 

 } 

 public void setLists(List<String> lists) { 

 this.lists = lists; 

 } 

 public Properties getProperties() { 

 return properties; 

 } 

 public void setProperties(Properties properties) { 

 this.properties = properties; 

 } 

 public Map<String, String> getMaps() { 

 return maps; 

 } 

 public void setMaps(Map<String, String> maps) { 

 this.maps = maps; 

 } 


 public PersonServer(){} 


 public void save() 

 { 

 //personDao.add(); 

 Iterator<String> iterator=sets.iterator(); 

 while (iterator.hasNext()) { 

 String string = (String) iterator.next(); 

 System.out.println(string); 

 } 

 System.out.println("---------------------"); 

 Iterator<String> iterator2= lists.iterator(); 

 while (iterator2.hasNext()) { 

 String string = (String) iterator2.next(); 

 System.out.println(string); 

 } 

 Set<Entry<Object, Object>> sets=properties.entrySet(); 

 Iterator<Entry<Object, Object>> iterator3=sets.iterator(); 

 System.out.println("---------------------"); 

 while (iterator3.hasNext()) { 

 Map.Entry<java.lang.Object, java.lang.Object> entry = (Map.Entry<java.lang.Object, java.lang.Object>) iterator3.next(); 

 System.out.println(entry.getKey()+"="+entry.getValue()); 

 } 

 System.out.println("---------------------"); 

 Set<Entry<String, String>> mapSet=maps.entrySet(); 

 Iterator<Entry<String, String>> iterator4=mapSet.iterator(); 

 while (iterator4.hasNext()) { 

 Map.Entry<java.lang.String, java.lang.String> entry = (Map.Entry<java.lang.String, java.lang.String>) iterator4 

 .next(); 

 System.out.println(entry.getKey()+"="+entry.getValue()); 

 } 


 } 

} 


第二步:编写beans.xml配置文件 

<?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-2.5.xsd" 
 default-lazy-init="false"> 

 <bean id="personServer" class="com.liyong.ServersBean.PersonServer"> 

 <property name="sets" > 

 <set> 

 <value>set1</value> 

 <value>set2</value> 

 </set> 

 </property> 

 <property name="lists" > 

 <list> 

 <value>list1</value> 

 <value>list2</value> 

 </list> 

 </property> 

 <property name="properties"> 

 <props> 

 <prop key="prop1">prop1</prop> 

 <prop key="prop2">prop2</prop> 

 <prop key="prop3">prop3</prop> 

 </props> 

 </property> 

 <property name="maps"> 

 <map> 

 <entry key="map1" value="map1"/> 

 <entry key="map2" value="map2"/> 

 <entry key="map3" value="map3"/> 

 </map> 

 </property> 

 </bean> 

</beans> 


第三步:编写单元测试 

public class JUnitTest { 


 @Test 

 public void TestSave() 

 { 

 //得到Spring容器实例、 


 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 

 //AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 

 //ApplicationContext ctx2 = new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); 


 //这里面向接口 

 IPersonServer server =(IPersonServer)ctx.getBean("personServer"); 

 server.save(); 

 } 

} 


第四步:测试...

举报

相关推荐

0 条评论