Spring Collections(List,Set,Map,Properties)

来源:互联网 发布:p2p监控软件 编辑:程序博客网 时间:2024/05/07 21:17

下面的例子这是的是spring注入集合(list,set,map,properties)

List---><list/>

Set---><set/>

Map--><map/>

Properties---><pros/>

在CollectionTest中有四种集合属性

com.lzyer.common下

package com.lzyer.common;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class CollectionTest {private List<Object> lists;private Set<Object> sets;private Map<Object,Object> maps;private Properties properties;public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Set<Object> getSets() {return sets;}public void setSets(Set<Object> sets) {this.sets = sets;}public Map<Object, Object> getMaps() {return maps;}public void setMaps(Map<Object, Object> maps) {this.maps = maps;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Overridepublic String toString() {return "CollectionTest [lists=" + lists + "\n, sets=" + sets + "\n, maps=" + maps + "\n, properties=" + properties+ "]";}}
一个javabean Person.java

package com.lzyer.common;public class Person {private String name;private String address;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person [name=" + name + ", address=" + address + ", age=" + age + "]";}}

Spring-Collections.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="collection" class="com.lzyer.common.CollectionTest">   <!-- java.util.List -->    <property name="lists">        <list>           <value>1</value>           <ref bean="person"/>           <bean class="com.lzyer.common.Person">             <property name="name" value="mair"></property>             <property name="address" value="shanghai"></property>             <property name="age" value="18"></property>           </bean>        </list>    </property>        <!-- java.util.Set -->    <property name="sets">    <set>       <value>1</value>           <ref bean="person"/>           <bean class="com.lzyer.common.Person">             <property name="name" value="mair"></property>             <property name="address" value="shanghai"></property>             <property name="age" value="18"></property>           </bean>    </set>    </property>    <!-- java.util.Map -->    <property name="maps">          <map>          <entry key="key 1" value="1111"/>          <entry key="key 2" value-ref="person"/>          <entry key="key 3">            <bean class="com.lzyer.common.Person">               <property name="name" value="hello"/>               <property name="address" value="world"/>               <property name="age" value="30"/>            </bean>          </entry>          </map>    </property>        <!-- java.util.Properties -->    <property name="properties">        <props>           <prop key="admin">admin@oraclecsg.com</prop>           <prop key="support">support@oraclecsg.com</prop>        </props>    </property></bean><bean id="person" class="com.lzyer.common.Person"><property name="name" value="lzyer"></property><property name="address" value="hunan"></property><property name="age" value="21"></property></bean></beans> 
测试类:CollectionApp.java

package com.lzyer.core;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lzyer.common.CollectionTest;public class CollectionApp {public static void main(String[] args) {@SuppressWarnings("resource")ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Collection.xml");CollectionTest ct = (CollectionTest)ac.getBean("collection");System.out.println(ct);}}
output:



文章参考:http://www.mkyong.com/spring/spring-collections-list-set-map-and-properties-example/





0 0