from: http://hi.baidu.com/winters1224/item/d5fe20bd873513afeaba9322
exactList是action里的一个List<String>类型的变量。
<property name="exactList">
<list>
<value>01</value>
<value>02</value>
<value>03</value>
<value>04</value>
<value>05</value>
<value>06</value>
<value>07</value>
<value>08</value>
<value>09</value>
<value>34</value>
</list>
</property>
<property name="exactList">
<list>
<value>01</value>
<value>02</value>
<value>03</value>
<value>04</value>
<value>05</value>
<value>06</value>
<value>07</value>
<value>08</value>
<value>09</value>
<value>34</value>
</list>
</property>
再附一个别人的例子
1.--User类
package org.stelcomtech;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map;
import java.util.Set;
public class User {
private String username;
private String username;
private int age;
private Home myHome;
private Home[] homes;
private List homesList;
private Set homesSet;
private Map homesMap;
public Home[] getHomes() {
return homes;
}
return homes;
}
public void setHomes(Home[] homes) {
this.homes = homes;
}
this.homes = homes;
}
public List getHomesList() {
return homesList;
}
return homesList;
}
public void setHomesList(List homesList) {
this.homesList = homesList;
}
this.homesList = homesList;
}
public Map getHomesMap() {
return homesMap;
}
return homesMap;
}
public void setHomesMap(Map homesMap) {
this.homesMap = homesMap;
}
this.homesMap = homesMap;
}
public Set getHomesSet() {
return homesSet;
}
return homesSet;
}
public void setHomesSet(Set homesSet) {
this.homesSet = homesSet;
}
this.homesSet = homesSet;
}
public Home getMyHome() {
return myHome;
}
return myHome;
}
public void setMyHome(Home myHome) {
this.myHome = myHome;
}
this.myHome = myHome;
}
public int getAge() {
return age;
}
return age;
}
public void setAge(int age) {
this.age = age;
}
this.age = age;
}
public String getUsername() {
return username;
}
return username;
}
public void setUsername(String username) {
this.username = username;
}
this.username = username;
}
}
2.---Home类
package org.stelcomtech;
public class Home {
private String homeAddr;
private String homeAddr;
public String getHomeAddr() {
return homeAddr;
}
return homeAddr;
}
public void setHomeAddr(String homeAddr) {
this.homeAddr = homeAddr;
}
this.homeAddr = homeAddr;
}
}
3.--测试类 MainTest
package org.stelcomtech;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MainTest {
public static void main(String[]args){
ApplicationContext ac=new FileSystemXmlApplicationContext("org/stelcomtech/applicationContext.xml");
User user=(User)ac.getBean("userid");
Home[] homes=user.getHomes();
System.out.println("************数组注入***************");
for(int i=0;i<homes.length;i++){
System.out.println("家庭地址:"+homes[i].getHomeAddr());
}
System.out.println("************List注入********************");
List list=user.getHomesList();
Iterator ir=list.iterator();
while(ir.hasNext()){
//Home home1=(Home)ir.next();
System.out.println("家庭地址:"+ir.next().toString());
}
System.out.println("***********Set注入******************");
Set set=user.getHomesSet();
Iterator ir1=set.iterator();
while(ir1.hasNext()){
System.out.println("家庭地址:"+ir1.next().toString());
}
System.out.println("************Map注入**************");
Map map=user.getHomesMap();
Home home11=(Home)map.get("1");
System.out.println("家庭地址:"+home11.getHomeAddr().toString());
Home home111=(Home)map.get("2");
System.out.println("家庭地址:"+home111.getHomeAddr().toString());
}
public static void main(String[]args){
ApplicationContext ac=new FileSystemXmlApplicationContext("org/stelcomtech/applicationContext.xml");
User user=(User)ac.getBean("userid");
Home[] homes=user.getHomes();
System.out.println("************数组注入***************");
for(int i=0;i<homes.length;i++){
System.out.println("家庭地址:"+homes[i].getHomeAddr());
}
System.out.println("************List注入********************");
List list=user.getHomesList();
Iterator ir=list.iterator();
while(ir.hasNext()){
//Home home1=(Home)ir.next();
System.out.println("家庭地址:"+ir.next().toString());
}
System.out.println("***********Set注入******************");
Set set=user.getHomesSet();
Iterator ir1=set.iterator();
while(ir1.hasNext()){
System.out.println("家庭地址:"+ir1.next().toString());
}
System.out.println("************Map注入**************");
Map map=user.getHomesMap();
Home home11=(Home)map.get("1");
System.out.println("家庭地址:"+home11.getHomeAddr().toString());
Home home111=(Home)map.get("2");
System.out.println("家庭地址:"+home111.getHomeAddr().toString());
}
}
4.配置文件 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-2.0.xsd">
<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-2.0.xsd">
<bean id="myHome1" class="org.stelcomtech.Home" abstract="false"
lazy-init="default" dependency-check="default">
<property name="homeAddr">
<value>wuhan</value>
</property>
</bean>
<bean id="myHome2" class="org.stelcomtech.Home" abstract="false"
lazy-init="default" dependency-check="default">
<property name="homeAddr">
<value>shanghai</value>
</property>
</bean>
lazy-init="default" dependency-check="default">
<property name="homeAddr">
<value>wuhan</value>
</property>
</bean>
<bean id="myHome2" class="org.stelcomtech.Home" abstract="false"
lazy-init="default" dependency-check="default">
<property name="homeAddr">
<value>shanghai</value>
</property>
</bean>
<!-- autowire 选择 byName时,按照Bean id 是否和类中的属性名来查找 -->
<!-- autowire 选择 byType时,按照 类的类型 来查找 -->
<bean id="userid" class="org.stelcomtech.User" abstract="false"
lazy-init="default" dependency-check="default">
<property name="username">
<value>haiyang</value>
</property>
<property name="age">
<value>22</value>
</property>
<!-- 数组注入方式 -->
<property name="homes">
<list>
<ref bean="myHome1" />
<ref bean="myHome2" />
</list>
</property>
<!-- List注入方式 -->
<property name="homesList">
<list>
<ref bean="myHome1" />
<ref bean="myHome2" />
<value>haha</value>
</list>
</property>
<!-- Set注入方式 -->
<property name="homesSet">
<set>
<value>hello</value>
<ref bean="myHome2" />
<ref bean="myHome1" />
</set>
</property>
<!-- Map注入方式 -->
<property name="homesMap">
<map>
<entry key="1">
<ref bean="myHome1" />
</entry>
<entry key="2">
<ref bean="myHome2" />
</entry>
</map>
</property>
</bean>
</beans>
</beans>
5.运行结果
log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
************数组注入***************
家庭地址:wuhan
家庭地址:shanghai
************List注入********************
家庭地址:
家庭地址:
家庭地址:haha
***********Set注入******************
家庭地址:hello
家庭地址:
家庭地址:
************Map注入**************
家庭地址:wuhan
家庭地址:shanghai
log4j:WARN Please initialize the log4j system properly.
************数组注入***************
家庭地址:wuhan
家庭地址:shanghai
************List注入********************
家庭地址:
家庭地址:
家庭地址:haha
***********Set注入******************
家庭地址:hello
家庭地址:
家庭地址:
************Map注入**************
家庭地址:wuhan
家庭地址:shanghai