通过<list/>、<set/>、<map/>及<props/>元素可以定义和设置与Java Collection类型对应List、Set、Map及Properties的值。下面通过一个简单例子说明一下:
CollectionService接口
package org.spring.service; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public interface CollectionService { public Set<String> getSets(); public List<String> getLists(); public Properties getProperties(); public Map<String, String> getMaps(); }
CollectionService的实现类CollectionServiceBean
package org.spring.service.impl; 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 org.spring.service.CollectionService; public class CollectionServiceBean implements CollectionService { 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; } }
spring.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"> <bean id="collectionServiceBean" class="org.spring.service.impl.CollectionServiceBean"> <property name="sets"> <set> <value>sets:First</value> <value>sets:Second</value> <value>sets:Third</value> </set> </property> <property name="lists"> <list> <value>lists:First</value> <value>lists:Second</value> <value>lists:Third</value> </list> </property> <property name="properties"> <props> <prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value3</prop> </props> </property> <property name="maps"> <map> <entry key="key-1" value="value-1"></entry> <entry key="key-2" value="value-2"></entry> <entry key="key-3" value="value-3"></entry> </map> </property> </bean> </beans>
测试类
package org.spring.junit; import org.junit.Test; import org.spring.service.CollectionService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringCollectionTest { @Test public void testCollection() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "spring.xml"); CollectionService collectionService = (CollectionService) ctx .getBean("collectionServiceBean"); System.out.println("============sets============"); for (String value : collectionService.getSets()) { System.out.println(value); } System.out.println("============lists============"); for (String value : collectionService.getLists()) { System.out.println(value); } System.out.println("============properties============"); for (Object key : collectionService.getProperties().keySet()) { System.out.println(key + " = " + collectionService.getProperties().getProperty( (String) key)); } System.out.println("============maps============"); for (Object key : collectionService.getMaps().keySet()) { System.out.println(key + " = " + collectionService.getMaps().get(key)); } } }
控制台输出结果:
相关推荐
在Spring框架中,集合类型的属性装配是常见的需求,如List、Set、Map等。这些集合在配置文件或注解中进行装配,可以帮助我们管理复杂的对象依赖关系。本篇将详细介绍Spring如何装配各种集合类型的属性。 1. **XML...
在Spring框架中,集合装配是将一组对象注入到如List、Set、Map等集合类型属性中的过程。这个过程是依赖注入(Dependency Injection,DI)的一个重要方面,它使得应用程序更加灵活,易于测试和维护。本篇文章将深入...
在Spring中可以装配4种集合类型属性:List、set、Map和Properties。与这四种集合对应的标签是、、、。CollectionBean是一个包含上述4种集合类型的JavaBean,代码如下:
在Spring框架的学习中,"装配各种集合类型的属性"是一个重要的概念,这涉及到如何将不同的集合对象,如List、Set、Map等,与Spring的依赖注入(Dependency Injection, DI)机制相结合,实现灵活的配置和管理。...
马程序员_黎活明__Spring如何装配各种集合类型的属性.ppt )