首页 > 代码库 > (二)装配各种集合类型的属性

(二)装配各种集合类型的属性

一、接口PersonService,复制get方法,便于得到相应集合

public interface PersonService {

	public void save();
	public Set<String> getSets();
	public List<String> getLists();
	public Properties getProperties();
	public Map<String, String> getMaps();
}

二、子类PersonServiceImpl

public class PersonServiceImpl implements PersonService {

	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 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;
	}
}

三、applicationContext.xml的配置

   <bean id="personService" class="com.lovo.u34.service.impl.PersonServiceImpl">
       <!--  set集合 -->
        <property name="sets">
        	<set>
        		<value>set第一个</value>
        		<value>set第二个</value>
        		<value>set第三个</value>
        	</set>
        </property>
       <!--  list集合 -->
       <property name="lists">
       		<list>
       			<value>list01</value>
       			<value>list02</value>
       			<value>list03</value>
       		</list>
       </property>
      <!--  properties -->
      <property name="properties">
      	    <props>
      	    	<prop key="key1">pro1</prop>
      	    	<prop key="key2">pro2</prop>
      	    	<prop key="key3">pro3</prop>
      	    </props>
      </property>
       <!-- map -->
       <property name="maps">
       		<map>
       			<entry key="key-1" value="http://www.mamicode.com/key1"></entry>
       			<entry key="key-2" value="http://www.mamicode.com/key2"></entry>
       			<entry key="key-3" value="http://www.mamicode.com/key3"></entry>
       		</map>
       </property>
    </bean>
</beans>

四、Test中打印

public class Test extends TestCase{

	public void testSave(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonService ps=(PersonService) ac.getBean("personService");
		for(String value:ps.getSets()){
			System.out.println(value);
		}
		for(String value:ps.getLists()){
			System.out.println(value);
		}
		for(Object value:ps.getProperties().keySet()){
			System.out.println(ps.getProperties().getProperty((String)value));
		}
		for(String value:ps.getMaps().keySet()){
			System.out.println(ps.getMaps().get(value));
		}
	}
}

  

  

  

  

(二)装配各种集合类型的属性