用数值配置spring装配的JavaBean内部的List类型很容易,下面介绍如何用javabean装配JavaBean中的List
public class Element implements Serializable{
/**
*
*/
private static final long serialVersionUID = -6956332143541075576L;
private Integer id;
private String name;
private String url;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
public class Test {
private List<Element> elementList;
public List<Element> getElementList() {
return elementList;
}
public void setElementList(List<Element> elementList) {
this.elementList = elementList;
}
/**
* @param args
*/
public static void main(String[] args) {
String[] configLocations = {"E:\\test.xml"};
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(configLocations);
Test test = (Test)applicationContext.getBean("test");
List<Element> elList = test.getElementList();
for(Element el : elList){
System.out.println(el.getId() + " , " + el.getName() + " , " + el.getUrl());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="element0" class="com.beantest.Element"> <property name="id" value="1001"/> <property name="name" value="hello"/> <property name="url" value="http://www.baidu.com/"/> </bean> <bean id="element1" class="com.beantest.Element"> <property name="id" value="1002"/> <property name="name" value="world"/> <property name="url" value="http://www.google.com/"/> </bean> <bean id="test" class="com.beantest.Test"> <property name="elementList"> <list> <ref bean="element0" /> <ref bean="element1" /> </list> </property> </bean> </beans>
这样,两个bean element0和element1就被装配到了bean test里面。
<bean id="moreComplexObject" class="example.ComplexObject"> <!-- results in a setAdminEmails(java.util.Properties) call --> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.org</prop> <prop key="support">support@example.org</prop> <prop key="development">development@example.org</prop> </props> </property> <!-- results in a setSomeList(java.util.List) call --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list> </property> <!-- results in a setSomeMap(java.util.Map) call --> <property name="someMap"> <map> <entry key="an entry" value="just some string"/> <entry key ="a ref" value-ref="myDataSource"/> </map> </property> <!-- results in a setSomeSet(java.util.Set) call --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set> </property> </bean>
spring直接在xml里面声明List,如下:
<bean id="dmapScheduleframeworkCallbackUrls" class="java.util.ArrayList"> <constructor-arg index="1"> <list> <value>localhost:8080</value> <value>localhost:8081</value> <value>localhost:8082</value> </list> </constructor-arg> </bean>