在此示例中,您将看到如何使用<set>元素连接bean的collection属性。我们将重用在上一个示例中使用的Bean。如何在Spring中使用list元素注入集合?
该Album豆有一个songs是有一个类型的属性java.util.List。该<set>元素不必与一起使用java.util.Set。它可用于连接java.util.List收藏集。这只是意味着它不能包含重复值,因此集合将仅包含唯一值。
这是我们如何配置Spring上下文:
<?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"> <set> <ref bean="song1"/> <ref bean="song1"/> <ref bean="song1"/> </set> </property> </bean> </beans>
该<set>配置可以在albumbean配置中看到。我们设置songs属性。在这个property元素中,我们使用<set>元素。然后使用<ref>元素将一些bean添加到集合中。
创建以下代码以运行它:
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 + '}'; } }
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; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DemoSet { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"collection-set.xml"}); Album album = (Album) context.getBean("album"); System.out.println("Album = " + album); } }
您将在屏幕上看到以下输出。如您所见,尽管我们在songs属性中设置了三个bean,但该Albumbean只包含一首歌曲。这是因为我们使用<set>元素来连接集合。它不允许重复值。
Album = Album{title='Please Please Me', year=1963, songs=[Song{title='I Saw Her Standing There', writer='Beatles'}], publisher={}, props={}}