1、前言
众所周知、java中不只有八大简单类型、还有一些集合类型、本文围绕集合类型的注入做一个总结。
2、项目骨架
3、过程
1、创建实体类AllCollectionType
package com.feng.entity;
import java.util.*;
public class AllCollectionType {
private List<String> listElement;
private String[] arrayElement;
private Set<String> setElement;
private Map<String,String> mapElement;
private Properties propsElement;
@Override
public String toString() {
return "AllCollectionType{" +
"listElement=" + listElement +
", arrayElement=" + Arrays.toString(arrayElement) +
", setElement=" + setElement +
", mapElement=" + mapElement +
", propsElement=" + propsElement +
'}';
}
public List<String> getListElement() {
return listElement;
}
public void setListElement(List<String> listElement) {
this.listElement = listElement;
}
public String[] getArrayElement() {
return arrayElement;
}
public void setArrayElement(String[] arrayElement) {
this.arrayElement = arrayElement;
}
public Set<String> getSetElement() {
return setElement;
}
public void setSetElement(Set<String> setElement) {
this.setElement = setElement;
}
public Map<String, String> getMapElement() {
return mapElement;
}
public void setMapElement(Map<String, String> mapElement) {
this.mapElement = mapElement;
}
public Properties getPropsElement() {
return propsElement;
}
public void setPropsElement(Properties propsElement) {
this.propsElement = propsElement;
}
}
2、配置applicationContext.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.xsd">
<bean id="collectionDemo" class="com.feng.entity.AllCollectionType">
<property name="listElement">
<list>
<value>足球</value>
<value>蓝球</value>
<value>乒乓球</value>
</list>
</property>
<property name="arrayElement">
<array>
<value>足球1</value>
<value>蓝球1</value>
<value>乒乓球1</value>
</array>
</property>
<property name="setElement">
<set>
<value>足球2</value>
<value>蓝球2</value>
<value>乒乓球2</value>
</set>
</property>
<property name="mapElement">
<map>
<entry>
<key>
<value>foot3</value>
</key>
<value>足球3</value>
</entry>
<entry>
<key>
<value>basket3</value>
</key>
<value>蓝球3</value>
</entry>
<entry>
<key>
<value>pp3</value>
</key>
<value>乒乓球3</value>
</entry>
</map>
</property>
<property name="propsElement">
<props>
<prop key="foot4">足球4</prop>
<prop key="basket4">蓝球4</prop>
<prop key="pp4">乒乓球4</prop>
</props>
</property>
</bean>
</beans>
3、编写测试类
package com.feng.test;
import com.feng.entity.AllCollectionType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void collectionDemo() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AllCollectionType type = (AllCollectionType)context.getBean("collectionDemo");
System.out.println(type);
}
public static void main(String[] args) {
collectionDemo();
}
}
4、运行结果
4、总结
本文简单讲述了四种集合属性的注入方式、对之前的注入方式进行了一次补充。