java_spring_List,Map,Properties,Set注入与遍历
package com.dao.bean.www; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public interface PersonServiceDao { public Map<String, String> getMaps(); public Properties getProperties(); public List<String> getLists(); public Set<String> getSets(); public abstract void save(); }
package com.bean.www; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import com.PersonDaoBean.test.PersonDao; import com.dao.bean.www.PersonServiceDao; public class PersonServiceBean implements PersonServiceDao { 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>(); public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public void save() { } }
//注入配置
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="personService" class="com.bean.www.PersonServiceBean"> <property name="sets"> <set> <value>Set-aaaaaaa</value> <value>Set-cccccccc</value> <value>Set-bbbb</value> <value>Set-bbbb</value> </set> </property> <property name="lists"> <list> <value>List-aaaaaaa</value> <value>List-cccccccc</value> <value>List-bbbb</value> <value>List-bbbb</value> </list> </property> <property name="properties"> <props> <prop key="key1">propties1</prop> <prop key="key2">propties2</prop> <prop key="key3">propties3</prop> </props> </property> <property name="maps"> <map> <entry key="mapsKey1" value="aaaaa-mapsValue1"></entry> <entry key="mapsKey2" value="cccccccc-mapsValue2"></entry> <entry key="mapsKey3" value="bbbbbbb-mapsValue3"></entry> </map> </property> </bean> </beans>
package com.itcast.www; import static org.junit.Assert.*; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dao.bean.www.PersonServiceDao; public class TestCaseDemo { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); PersonServiceDao personService = (PersonServiceDao) ctx .getBean("personService"); for (String value : personService.getSets()) { System.out.println(value); } System.out.println("*********************************"); for (String value : personService.getLists()) { System.out.println(value); } System.out.println("*********************************"); for (Object key : personService.getProperties().keySet()) { System.out.println(key + " = " + personService.getProperties().getProperty((String) key)); } System.out.println("*********************************"); for (String key : personService.getMaps().keySet()) { System.out.println(key + " = " + personService.getMaps().get(key)); } } }