最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Spring——IOC各种类型的集合注入

互联网 admin 0浏览 0评论

Spring——IOC各种类型的集合注入

首先创建一个类,包含List、array、set、map、properties属性

AllCollectionType.java

public class AllCollectionType {private List<String> listCollection;private String[] arrayCollection;private Set<String> setCollection;private Map<String,String> mapCollection;private Properties propsCollection;public List<String> getListCollection() {return listCollection;}public void setListCollection(List<String> listCollection) {this.listCollection = listCollection;}public String[] getArrayCollection() {return arrayCollection;}public void setArrayCollection(String[] arrayCollection) {this.arrayCollection = arrayCollection;}public Set<String> getSetCollection() {return setCollection;}public void setSetCollection(Set<String> setCollection) {this.setCollection = setCollection;}public Map<String, String> getMapCollection() {return mapCollection;}public void setMapCollection(Map<String, String> mapCollection) {this.mapCollection = mapCollection;}public Properties getPropsCollection() {return propsCollection;}public void setPropsCollection(Properties propsCollection) {this.propsCollection = propsCollection;}@Overridepublic String toString() {return "AllCollectionType [listCollection=" + listCollection + ", arrayCollection="+ Arrays.toString(arrayCollection) + ", setCollection=" + setCollection + ", mapCollection="+ mapCollection + ", propsCollection=" + propsCollection + "]";}}

applicationContext.xml

<bean id="Collection" class="com.dt.entity.AllCollectionType"><property name="listCollection"><list><value>苹果1</value><value>菠萝1</value><value>西瓜1</value></list></property><property name="arrayCollection"><array><value>苹果2</value><value>菠萝2</value><value>西瓜2</value></array></property><property name="setCollection"><set><value>苹果3</value><value>菠萝3</value><value>西瓜3</value></set></property><property name="mapCollection"><map><entry><key><value>apple</value></key><value>苹果4</value></entry><entry><key><value>pineapple </value></key><value>菠萝4</value></entry><entry><key><value>waterlmeon</value></key><value>西瓜4</value></entry></map></property><property name="propsCollection"><props><prop key="apple5">苹果5</prop><prop key="pineapple5">菠萝5</prop><prop key="watermelon5">西瓜5</prop></props></property></bean>

测试类test.java

public class test {public static void main(String[] args) {// TODO Auto-generated method stubcollection();}//测试集合类型public static void collection(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");AllCollectionType type =(AllCollectionType)context.getBean("Collection");System.out.println(type);}}

 

Spring——IOC各种类型的集合注入

首先创建一个类,包含List、array、set、map、properties属性

AllCollectionType.java

public class AllCollectionType {private List<String> listCollection;private String[] arrayCollection;private Set<String> setCollection;private Map<String,String> mapCollection;private Properties propsCollection;public List<String> getListCollection() {return listCollection;}public void setListCollection(List<String> listCollection) {this.listCollection = listCollection;}public String[] getArrayCollection() {return arrayCollection;}public void setArrayCollection(String[] arrayCollection) {this.arrayCollection = arrayCollection;}public Set<String> getSetCollection() {return setCollection;}public void setSetCollection(Set<String> setCollection) {this.setCollection = setCollection;}public Map<String, String> getMapCollection() {return mapCollection;}public void setMapCollection(Map<String, String> mapCollection) {this.mapCollection = mapCollection;}public Properties getPropsCollection() {return propsCollection;}public void setPropsCollection(Properties propsCollection) {this.propsCollection = propsCollection;}@Overridepublic String toString() {return "AllCollectionType [listCollection=" + listCollection + ", arrayCollection="+ Arrays.toString(arrayCollection) + ", setCollection=" + setCollection + ", mapCollection="+ mapCollection + ", propsCollection=" + propsCollection + "]";}}

applicationContext.xml

<bean id="Collection" class="com.dt.entity.AllCollectionType"><property name="listCollection"><list><value>苹果1</value><value>菠萝1</value><value>西瓜1</value></list></property><property name="arrayCollection"><array><value>苹果2</value><value>菠萝2</value><value>西瓜2</value></array></property><property name="setCollection"><set><value>苹果3</value><value>菠萝3</value><value>西瓜3</value></set></property><property name="mapCollection"><map><entry><key><value>apple</value></key><value>苹果4</value></entry><entry><key><value>pineapple </value></key><value>菠萝4</value></entry><entry><key><value>waterlmeon</value></key><value>西瓜4</value></entry></map></property><property name="propsCollection"><props><prop key="apple5">苹果5</prop><prop key="pineapple5">菠萝5</prop><prop key="watermelon5">西瓜5</prop></props></property></bean>

测试类test.java

public class test {public static void main(String[] args) {// TODO Auto-generated method stubcollection();}//测试集合类型public static void collection(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");AllCollectionType type =(AllCollectionType)context.getBean("Collection");System.out.println(type);}}

 

发布评论

评论列表 (0)

  1. 暂无评论