Spring Collections (List, Set, Map, and Properties) example
来源:互联网 发布:新白狐网络歌手 编辑:程序博客网 时间:2024/05/19 22:49
Spring examples to show you how to inject values into collections type (List
, Set
, Map
, and Properties
). 4 major collection types are supported :
- List –
- Set –
- Map –
Spring beans
A Customer object, with four collection properties.
package com.mkyong.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; //...}
See different code snippets to declare collection in bean configuration file.
1. List example
<property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property>
2. Set example
<property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property>
3. Map example
<property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </property>
4. Properties example
<property name="pros"> <props> <prop key="admin">admin@nospam.com</prop> <prop key="support">support@nospam.com</prop> </props> </property>
Full Spring’s bean configuration file.
<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.mkyong.common.Customer"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> <!-- java.util.Map --> <property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </property> <!-- java.util.Properties --> <property name="pros"> <props> <prop key="admin">admin@nospam.com</prop> <prop key="support">support@nospam.com</prop> </props> </property> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean></beans>
Run it…
package com.mkyong.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("SpringBeans.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); }}
Output
Customer [lists=[1, Person [address=address 1, age=28, name=mkyong1], Person [address=address, age=28, name=mkyongList]], maps={key 1=1,key 2=Person [address=address 1, age=28, name=mkyong1], key 3=Person [address=address, age=28, name=mkyongMap]}, pros={admin=admin@nospam.com, support=support@nospam.com}, sets=[1, Person [address=address 1, age=28, name=mkyong1], Person [address=address, age=28, name=mkyongSet]]]
0 0
- Spring(6)-Spring Collections (List, Set, Map, and Properties) example
- Spring Collections (List, Set, Map, and Properties) example
- Spring Collections(List,Set,Map,Properties)
- Spring Properties List Set Map
- spring注入Set,List,Properties和Map
- Spring 注入 List Set Properties Map
- Spring注入Properties、List、Map、Set
- Spring集合 (List,Set,Map,Properties) 实例
- (3) spring注入Properties,List,Set,Map
- List,Set,Map,Properties的Spring注入实例
- List,Set,Map,Properties的Spring注入实例
- List,Set,Map,Properties的Spring注入实例
- Spring中的集合——List、Map、Set、Properties
- Spring注入容器类型List、Map、Properties、Set
- Spring注入集合的值List,Set,Map,Properties
- Spring中注入List Set Map Properties的配置文件
- Set, List And Map
- java collections framework -----List,Set,Map总结
- Android studio Push项目到Github
- 利用Eclipse使用Java OpenCV(Using OpenCV Java with Eclipse)
- 读懂Java中的Socket编程
- log4j配置(一)
- 石材价格计算方式与选手方法
- Spring Collections (List, Set, Map, and Properties) example
- 数据库的基本概念
- centos linux系统下搭建git服务器
- flex生命周期序列图
- Office 365 IT Professional——批量用户创建
- poj1743(后缀数组)
- CATransform3D-Cube
- Android HandlerThread 完全解析
- hdu2955-Robberies(01背包 概率 )