在Spring中配置Collection

2007-12-23 10:35:42  作者
///////////////////////////////////////////////////////////////////////////////////////
//File:beans.XML

%26lt;!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"%26gt;
%26lt;beans%26gt;
%26lt;!--Oraclebeanusedforafewexamples--%26gt;
%26lt;beanid="oracle"name="wiseworm"class="BookwormOracle"/%26gt;

%26lt;!--collectioninjectionsamples--%26gt;
%26lt;beanid="injectCollection"class="CollectionInjection"%26gt;
%26lt;propertyname="map"%26gt;
%26lt;map%26gt;
%26lt;entrykey="someValue"%26gt;
%26lt;value%26gt;HelloWorld!%26lt;/value%26gt;
%26lt;/entry%26gt;
%26lt;entrykey="someBean"%26gt;
%26lt;reflocal="oracle"/%26gt;
%26lt;/entry%26gt;
%26lt;/map%26gt;
%26lt;/property%26gt;
%26lt;propertyname="props"%26gt;
%26lt;props%26gt;
%26lt;propkey="firstName"%26gt;Rob%26lt;/prop%26gt;
%26lt;propkey="secondName"%26gt;Harrop%26lt;/prop%26gt;
%26lt;/props%26gt;
%26lt;/property%26gt;
%26lt;propertyname="set"%26gt;
%26lt;set%26gt;
%26lt;value%26gt;HelloWorld!%26lt;/value%26gt;
%26lt;reflocal="oracle"/%26gt;
%26lt;/set%26gt;
%26lt;/property%26gt;
%26lt;propertyname="list"%26gt;
%26lt;list%26gt;
%26lt;value%26gt;HelloWorld!%26lt;/value%26gt;
%26lt;reflocal="oracle"/%26gt;
%26lt;/list%26gt;
%26lt;/property%26gt;
%26lt;/bean%26gt;
%26lt;/beans%26gt;

///////////////////////////////////////////////////////////////////////////////////////

publicinterfaceOracle{

publicStringdefineMeaningOfLife();
}

///////////////////////////////////////////////////////////////////////////////////////
publicclassEncyclopedia{

}



///////////////////////////////////////////////////////////////////////////////////////

publicclassBookwormOracleimplementsOracle{

privateEncyclopediaenc;

publicvoidsetEncyclopedia(Encyclopediaenc){
this.enc=enc;
}

publicStringdefineMeaningOfLife(){
return"Encyclopedia'sareawasteofmoney-usetheInternet";
}

}

///////////////////////////////////////////////////////////////////////////////////////
importJava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Properties;
importjava.util.Set;

importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.xml.XmlBeanFactory;
importorg.springframework.core.io.FileSystemResource;

publicclassCollectionInjection{

privateMapmap;

privatePropertiesprops;

privateSetset;

privateListlist;

publicstaticvoidmain(String[]args){
BeanFactoryfactory=newXmlBeanFactory(newFileSystemResource(
"build/beans.xml"));

CollectionInjectioninstance=(CollectionInjection)factory.getBean("injectCollection");
instance.displayInfo();
}

publicvoidsetList(Listlist){
this.list=list;
}

publicvoidsetSet(Setset){
this.set=set;
}

publicvoidsetMap(Mapmap){
this.map=map;
}

publicvoidsetProps(Propertiesprops){
this.props=props;
}

publicvoiddisplayInfo(){

//displaytheMap
Iteratori=map.keySet().iterator();

System.out.println("Mapcontents:\n");
while(i.hasNext()){
Objectkey=i.next();
System.out.println("Key:"+key+"-Value:"+map.get(key));
}

//displaytheproperties
i=props.keySet().iterator();
System.out.println("\nPropertiescontents:\n");
while(i.hasNext()){
Stringkey=i.next().toString();
System.out.println("Key:"+key+"-Value:"
+props.getProperty(key));
}

//displaytheset
i=set.iterator();
System.out.println("\nSetcontents:\n");
while(i.hasNext()){
System.out.println("Value:"+i.next());
}

//displaythelist
i=list.iterator();
System.out.println("\nListcontents:\n");
while(i.hasNext()){
System.out.println("Value:"+i.next());
}
}
}

(出处http://www.knowsky.com)