首页 > 代码库 > Spring集合 (List,Set,Map,Properties) 实例
Spring集合 (List,Set,Map,Properties) 实例
下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties)。 支持4个主要的集合类型:
- List – <list/>
- Set – <set/>
- Map – <map/>
- Properties – <props/>
Spring beans
一个Customer对象,有四个集合属性。
package com.yiibai.common;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class Customer { private List<Object> lists; private Set<Object> sets; private Map<Object, Object> maps; private Properties pros; //...}
在bean配置文件中不同的代码片段用来声明集合。
1. List示例
<property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.yiibai.common.Person"> <property name="name" value="http://www.mamicode.com/yiibaiList" /> <property name="address" value="http://www.mamicode.com/Hainan" /> <property name="age" value="http://www.mamicode.com/28" /> </bean> </list> </property>
2. Set示例
<property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.yiibai.common.Person"> <property name="name" value="http://www.mamicode.com/yiibaiSet" /> <property name="address" value="http://www.mamicode.com/Hainan" /> <property name="age" value="http://www.mamicode.com/28" /> </bean> </set> </property>
3. Map示例
<property name="maps"> <map> <entry key="Key 1" value="http://www.mamicode.com/1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.yiibai.common.Person"> <property name="name" value="http://www.mamicode.com/yiibaiMap" /> <property name="address" value="http://www.mamicode.com/Hainan" /> <property name="age" value="http://www.mamicode.com/28" /> </bean> </entry> </map> </property>
4. Properties示例
<property name="pros"> <props> <prop key="admin">admin@yiibai.com</prop> <prop key="support">support@yiibai.com</prop> </props> </property>
Spring完整的 bean 配置文件。
<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="CustomerBean" class="com.yiibai.common.Customer"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.yiibai.common.Person"> <property name="name" value="http://www.mamicode.com/yiibaiList" /> <property name="address" value="http://www.mamicode.com/Hainan Haikou" /> <property name="age" value="http://www.mamicode.com/28" /> </bean> </list> </property> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.yiibai.common.Person"> <property name="name" value="http://www.mamicode.com/yiibaiSet" /> <property name="address" value="http://www.mamicode.com/Hainan Haikou" /> <property name="age" value="http://www.mamicode.com/28" /> </bean> </set> </property> <!-- java.util.Map --> <property name="maps"> <map> <entry key="Key 1" value="http://www.mamicode.com/1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.yiibai.common.Person"> <property name="name" value="http://www.mamicode.com/yiibaiMap" /> <property name="address" value="http://www.mamicode.com/Hainan Haikou" /> <property name="age" value="http://www.mamicode.com/28" /> </bean> </entry> </map> </property> <!-- java.util.Properties --> <property name="pros"> <props> <prop key="admin">admin@yiibai.com</prop> <prop key="support">support@yiibai.com</prop> </props> </property> </bean> <bean id="PersonBean" class="com.yiibai.common.Person"> <property name="name" value="http://www.mamicode.com/yiibai1" /> <property name="address" value="http://www.mamicode.com/Hainan Haikou 1" /> <property name="age" value="http://www.mamicode.com/28" /> </bean></beans>
执行程序
package com.yiibai.common;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); }}
输出
Customer [lists=[1, com.yiibai.common.Person@4e4ee70b, com.yiibai.common.Person@1e1867d2], sets=[1, com.yiibai.common.Person@4e4ee70b, com.yiibai.common.Person@52f644b4], maps={Key 1=1, Key 2=com.yiibai.common.Person@4e4ee70b, Key 3=com.yiibai.common.Person@54481b6d}, pros={admin=admin@yiibai.com, support=support@yiibai.com}]
下载代码 – http://pan.baidu.com/s/1c0T3i5i
Spring集合 (List,Set,Map,Properties) 实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。