java中的集合类型有:
Collection
ArrayList
Vector
Set
List
Map
HashMap
HashTable
LinkedList
我们需要了解当使用bean时如何给一个class 的 集合属性注入值
例如,下面的class 我们需要给这个class 的arrayList注入值
package com.commons.Service;
import java.text.MessageFormat;
import java.util.ArrayList;
public class User {
private String username;
private int age;
private String email;
private int id;
private String group;
private String phone;
private ArrayList arrayList;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void getUserInfo(){
System.out.println(MessageFormat.format("用户名是:{0} id是:{1} 所属组是:{2} 邮箱是:{3} 电话是:{4}",
this.username, this.id, this.age, this.group, this.email, this.phone));
}
public ArrayList getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList arrayList) {
this.arrayList = arrayList;
}
}
配置bean 并设置给集合添加值
<bean id="user" class="com.commons.Service.User">
<property name="username">
<value>sunshine</value>
</property>
<property name="age">
<value>88</value>
</property>
<property name="id">
<value>66</value>
</property>
<property name="email">
<value>[email protected]</value>
</property>
<property name="group">
<value>dev</value>
</property>
<property name="phone">
<value>123456</value>
</property>
<property name="arrayList">
<array>
<value>第一个元素</value>
<value>第二个元素</value>
<value>第三个元素</value>
</array>
</property>
</bean>
给泛型集合注入值。
假如你的bean 里面的集合类型是泛型的,那么你在注入的时候,就需要使用ref
例如:
package com.commons.Service;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
public class User {
private String username;
private int age;
private String email;
private int id;
private String group;
private String phone;
private ArrayList<String> arrayList;
// 这是一个泛型的集合
private List<Group> groupList;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void getUserInfo(){
System.out.println(MessageFormat.format("用户名是:{0} id是:{1} 所属组是:{2} 邮箱是:{3} 电话是:{4}",
this.username, this.id, this.age, this.group, this.email, this.phone));
}
public ArrayList getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList arrayList) {
this.arrayList = arrayList;
}
public List<Group> getGroupList() {
return groupList;
}
public void setGroupList(List<Group> groupList) {
this.groupList = groupList;
}
}
注入值:
<bean id="user" class="com.commons.Service.User">
<property name="username">
<value>sunshine</value>
</property>
<property name="age">
<value>88</value>
</property>
<property name="id">
<value>66</value>
</property>
<property name="email">
<value>[email protected]</value>
</property>
<property name="group">
<value>dev</value>
</property>
<property name="phone">
<value>123456</value>
</property>
<property name="arrayList">
<!--suppress InjectionValueTypeInspection -->
<list>
<value>第一个元素</value>
<value>第二个元素</value>
<value>第三个元素</value>
</list>
</property>
<property name="groupList">
<list>
<ref bean="groups"></ref>
<ref bean="group2"></ref>
<ref bean="group3"></ref>
</list>
</property>
</bean>
Set 集合类型注入
package com.commons.Service;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class User {
private String username;
private int age;
private String email;
private int id;
private String group;
private String phone;
private ArrayList<String> arrayList;
private List<Group> groupList;
private Set<Group> set;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void getUserInfo(){
System.out.println(MessageFormat.format("用户名是:{0} id是:{1} 所属组是:{2} 邮箱是:{3} 电话是:{4}",
this.username, this.id, this.age, this.group, this.email, this.phone));
}
public ArrayList getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList arrayList) {
this.arrayList = arrayList;
}
public List<Group> getGroupList() {
return groupList;
}
public void setGroupList(List<Group> groupList) {
this.groupList = groupList;
}
public Set<Group> getSet() {
return set;
}
public void setSet(Set<Group> set) {
this.set = set;
}
}
配置bean
<?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="helloWorld" class="com.commons.Service.HelloWorldSpring">
<property name="name">
<value>Tom</value>
</property>
<property name="age">
<value>88</value>
</property>
</bean>
<bean id="user" class="com.commons.Service.User">
<property name="username">
<value>sunshine</value>
</property>
<property name="age">
<value>88</value>
</property>
<property name="id">
<value>66</value>
</property>
<property name="email">
<value>[email protected]</value>
</property>
<property name="group">
<value>dev</value>
</property>
<property name="phone">
<value>123456</value>
</property>
<property name="arrayList">
<!--suppress InjectionValueTypeInspection -->
<list>
<value>第一个元素</value>
<value>第二个元素</value>
<value>第三个元素</value>
</list>
</property>
<property name="groupList">
<list>
<ref bean="groups"></ref>
<ref bean="group2"></ref>
<ref bean="group3"></ref>
</list>
</property>
<property name="set">
<set>
<ref bean="groups"></ref>
<ref bean="group2"></ref>
<ref bean="group3"></ref>
</set>
</property>
</bean>
<bean id="groups" class="com.commons.Service.Group">
<property name="id">
<value>1</value>
</property>
<property name="name">
<value>apache</value>
</property>
<property name="address">
<value>usa</value>
</property>
</bean>
<bean id="group2" class="com.commons.Service.Group">
<property name="name">
<value>alibaba</value>
</property>
<property name="address">
<value>china</value>
</property>
<property name="id">
<value>2</value>
</property>
</bean>
<bean id="group3" class="com.commons.Service.Group">
<property name="id">
<value>3</value>
</property>
<property name="name">
<value>redhat</value>
</property>
<property name="address">
<value>usa</value>
</property>
</bean>
</beans>
Map 集合类型注入
package com.commons.Service;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class User {
private String username;
private int age;
private String email;
private int id;
private String group;
private String phone;
private ArrayList<String> arrayList;
private List<Group> groupList;
private Set<Group> set;
private Map<Integer, Group> groupMap;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void getUserInfo(){
System.out.println(MessageFormat.format("用户名是:{0} id是:{1} 所属组是:{2} 邮箱是:{3} 电话是:{4}",
this.username, this.id, this.age, this.group, this.email, this.phone));
}
public ArrayList getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList arrayList) {
this.arrayList = arrayList;
}
public List<Group> getGroupList() {
return groupList;
}
public void setGroupList(List<Group> groupList) {
this.groupList = groupList;
}
public Set<Group> getSet() {
return set;
}
public void setSet(Set<Group> set) {
this.set = set;
}
public Map<Integer, Group> getGroupMap() {
return groupMap;
}
public void setGroupMap(Map<Integer, Group> groupMap) {
this.groupMap = groupMap;
}
}
配置bean
<?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="helloWorld" class="com.commons.Service.HelloWorldSpring">
<property name="name">
<value>Tom</value>
</property>
<property name="age">
<value>88</value>
</property>
</bean>
<bean id="user" class="com.commons.Service.User">
<property name="username">
<value>sunshine</value>
</property>
<property name="age">
<value>88</value>
</property>
<property name="id">
<value>66</value>
</property>
<property name="email">
<value>[email protected]</value>
</property>
<property name="group">
<value>dev</value>
</property>
<property name="phone">
<value>123456</value>
</property>
<property name="arrayList">
<!--suppress InjectionValueTypeInspection -->
<list>
<value>第一个元素</value>
<value>第二个元素</value>
<value>第三个元素</value>
</list>
</property>
<property name="groupList">
<list>
<ref bean="groups"></ref>
<ref bean="group2"></ref>
<ref bean="group3"></ref>
</list>
</property>
<property name="set">
<set>
<ref bean="groups"></ref>
<ref bean="group2"></ref>
<ref bean="group3"></ref>
</set>
</property>
<property name="groupMap">
<map>
<entry key="1" value-ref="groups"></entry>
<entry key="2" value-ref="group2"></entry>
</map>
</property>
</bean>
<bean id="groups" class="com.commons.Service.Group">
<property name="id">
<value>1</value>
</property>
<property name="name">
<value>apache</value>
</property>
<property name="address">
<value>usa</value>
</property>
</bean>
<bean id="group2" class="com.commons.Service.Group">
<property name="name">
<value>alibaba</value>
</property>
<property name="address">
<value>china</value>
</property>
<property name="id">
<value>2</value>
</property>
</bean>
<bean id="group3" class="com.commons.Service.Group">
<property name="id">
<value>3</value>
</property>
<property name="name">
<value>redhat</value>
</property>
<property name="address">
<value>usa</value>
</property>
</bean>
</beans>