注入除了一般型態外,也可針對Collection(List、Map、Set、Properties)進行注入。
Customer.java
package com.example ;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Customer {
private List nameList ;
private Map nameMap ;
private Set nameSet ;
private Properties nameProp ;
public List getNameList() {
return nameList;
}
public void setNameList(List nameList) {
this.nameList = nameList;
}
public Map getNameMap() {
return nameMap;
}
public void setNameMap(Map nameMap) {
this.nameMap = nameMap;
}
public Set getNameSet() {
return nameSet;
}
public void setNameSet(Set nameSet) {
this.nameSet = nameSet;
}
public Properties getNameProp() {
return nameProp;
}
public void setNameProp(Properties nameProp) {
this.nameProp = nameProp;
}
}
Main.java
package com.example ;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
Customer cust = (Customer) context.getBean("Customer");
System.out.println("name list:"+cust.getNameList()) ;
System.out.println("name map:"+cust.getNameMap()) ;
System.out.println("name set:"+cust.getNameSet()) ;
System.out.println("name property:"+cust.getNameProp()) ;
}
}
Map和Properties均屬於key-value paired,兩者差異在於Map key和value可以為任何物件型態,而Properties key和value只能為String型態。
Beans.xml
<bean id="Customer" class="com.example.Customer" >
<!-- java.util.List -->
<property name="nameList">
<list>
<value>Paul</value>
<value>Alice</value>
<value>Marry</value>
</list>
</property>
<!-- java.util.Map -->
<property name="nameMap">
<map>
<entry key="1" value="Paul"/>
<entry key="2" value="Alice"/>
<entry key="3" value="Marry"/>
<entry key="1" value="Paul"/>
</map>
</property>
<!-- java.util.Set -->
<property name="nameSet">
<set>
<value>Paul</value>
<value>Alice</value>
<value>Marry</value>
<value>Paul</value>
</set>
</property>
<!-- java.util.Property -->
<property name="nameProp">
<props>
<prop key="1">Paul</prop>
<prop key="2">Alice</prop>
<prop key="3">Marry</prop>
<prop key="1">Paul</prop>
</props>
</property>
</bean>
結果顯示如下所示:
name list:[Paul, Alice, Marry]
name map:{1=Paul, 2=Alice, 3=Marry}
name set:[Paul, Alice, Marry]
name property:{3=Marry, 2=Alice, 1=Paul}
接著混合ref bean的情況如下所示:
CustomerRef.java
package com.example;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CustomerRef {
private List lists;
private Set sets;
private Map maps;
private Properties pros;
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}
}
Person.java
package com.example;
public class Person {
String name;
String address;
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;
}
public String toString(){
return "name:"+name+",address:"+address+",age:"+age ;
}
}
MainRef.java
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainRef {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"BeansRef.xml");
CustomerRef cust = (CustomerRef) context.getBean("CustomerBean");
System.out.println(cust.getLists());
System.out.println(cust.getMaps());
System.out.println(cust.getSets());
System.out.println(cust.getPros());
}
}
BeansRef.xml
<!-- java.util.List -->
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.example.Person">
<property name="name" value="Steve" />
<property name="address" value="American" />
<property name="age" value="28" />
</bean>
</list>
</property>
<!-- java.util.Set -->
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.example.Person">
<property name="name" value="Steve" />
<property name="address" value="American" />
<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.example.Person">
<property name="name" value="Steve" />
<property name="address" value="American" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property>
<!-- java.util.Properties -->
<property name="pros">
<props>
<prop key="1">Paul</prop>
<prop key="2">Alice</prop>
<prop key="3">Marry</prop>
</props>
</property>
</bean>
<bean id="PersonBean" class="com.example.Person">
<property name="name" value="Paul" />
<property name="address" value="Taipei" />
<property name="age" value="31" />
</bean>
結果顯示如下所示: [1, name:Paul,address:Taipei,age:31, name:Steve,address:American,age:28]
{Key 1=1, Key 2=name:Paul,address:Taipei,age:31, Key 3=name:Steve,address:American,age:28}
[1, name:Paul,address:Taipei,age:31, name:Steve,address:American,age:28]
{3=Marry, 2=Alice, 1=Paul}
Injecting null and empty string values
等同於setName("") ;
<bean id="..." class="...">
<property name="name" value=""/>
</bean>
等同於setName(null) ;
<bean id="..." class="...">
<property name="name"><null/></property>
</bean>