下面的示例向您展示如何使用<list>元素来关联集合属性。我们可以使用它来连接数组或java.util.Collection诸如的某些实现的属性java.util.ArrayList。
在本示例中,我们将创建一个名为的bean Album,其中包含一个bean集合Song。这是我们的bean类。
package org.nhooo.example.spring.collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Album {
private String title;
private int year;
private List<Song> songs = new ArrayList<>();
private Map<String, Publisher> publisher = new HashMap<>();
private Properties props = new Properties();
public Album() {
}
public void setTitle(String title) {
this.title = title;
}
public void setYear(int year) {
this.year = year;
}
public void setSongs(List<Song> songs) {
this.songs = songs;
}
public void setPublisher(Map<String, Publisher> publisher) {
this.publisher = publisher;
}
public void setProps(Properties props) {
this.props = props;
}
@Override
public String toString() {
return "Album{" +
"title='" + title + '\'' +
", year=" + year +
", songs=" + songs +
", publisher=" + publisher +
", props=" + props +
'}';
}
}package org.nhooo.example.spring.collection;
public class Song {
private String title;
private String writer;
public Song() {
}
public void setTitle(String title) {
this.title = title;
}
public void setWriter(String writer) {
this.writer = writer;
}
@Override
public String toString() {
return "Song{" +
"title='" + title + '\'' +
", writer='" + writer + '\'' +
'}';
}
}package org.nhooo.example.spring.collection;
public class Publisher {
private String name;
public Publisher() {
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Publisher{" +
"name=" + name +
'}';
}
}这是Spring配置文件CollectionList.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="song1"> <property name="title" value="I Saw Her Standing There" /> <property name="writer" value="Beatles" /> </bean> <bean id="song2"> <property name="title" value="Misery" /> <property name="writer" value="Beatles" /> </bean> <bean id="song3"> <property name="title" value="Anna (Go to Him)" /> <property name="writer" value="Beatles" /> </bean> <bean id="album"> <property name="title" value="Please Please Me"/> <property name="year" value="1963"/> <property name="songs"> <list> <ref bean="song1"/> <ref bean="song2"/> <ref bean="song3"/> </list> </property> </bean> </beans>
连接歌曲集合的配置部分在albumbean内部。您可以看到我们有一个属性名称songs。该属性具有一个<list>元素,其中包含几个<ref>引用某些Song类型bean的元素。
现在,使用以下代码运行它:
package org.nhooo.example.spring.collection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoList {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"collection-list.xml"});
Album album = (Album) context.getBean("album");
System.out.println("Album = " + album);
}
}运行该程序时,您将看到以下输出:
Album = Album{title='Please Please Me', year=1963, songs=[Song{title='I Saw Her Standing There', writer='Beatles'}, Song{title='Misery', writer='Beatles'}, Song{title='Anna (Go to Him)', writer='Beatles'}], publisher={}, props={}}