在Spring中配置Collection
2007-12-23 12:35:24 来源:WEB开发网核心提示:/File: beans.xml<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>&l
///////////////////////////////////////////////////////////////////////////////////////
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Oracle bean used for a few examples -->
<bean id="oracle" name="wiseworm" class="BookwormOracle"/>
<!-- collection injection samples -->
<bean id="injectCollection" class="CollectionInjection">
<property name="map">
<map>
<entry key="someValue">
<value>Hello World!</value>
</entry>
<entry key="someBean">
<ref local="oracle"/>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="firstName"> Rob </prop>
<prop key="secondName"> Harrop </prop>
</props>
</property>
<property name="set">
<set>
<value>Hello World!</value>
<ref local="oracle"/>
</set>
</property>
<property name="list">
<list>
<value>Hello World!</value>
<ref local="oracle"/>
</list>
</property>
</bean>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
public interface Oracle {
public String defineMeaningOfLife();
}
///////////////////////////////////////////////////////////////////////////////////////
public class Encyclopedia {
}
///////////////////////////////////////////////////////////////////////////////////////
public class BookwormOracle implements Oracle {
private Encyclopedia enc;
public void setEncyclopedia(Encyclopedia enc) {
this.enc = enc;
}
public String defineMeaningOfLife() {
return "Encyclopedia's are a waste of money - use the Internet";
}
}
///////////////////////////////////////////////////////////////////////////////////////
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class CollectionInjection {
private Map map;
private Properties props;
private Set set;
private List list;
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"));
CollectionInjection instance = (CollectionInjection) factory.getBean("injectCollection");
instance.displayInfo();
}
public void setList(List list) {
this.list = list;
}
public void setSet(Set set) {
this.set = set;
}
public void setMap(Map map) {
this.map = map;
}
public void setProps(Properties props) {
this.props = props;
}
public void displayInfo() {
// display the Map
Iterator i = map.keySet().iterator();
System.out.println("Map contents:\n");
while (i.hasNext()) {
Object key = i.next();
System.out.println("Key: " + key + " - Value: " + map.get(key));
}
// display the properties
i = props.keySet().iterator();
System.out.println("\nProperties contents:\n");
while (i.hasNext()) {
String key = i.next().toString();
System.out.println("Key: " + key + " - Value: "
+ props.getProperty(key));
}
// display the set
i = set.iterator();
System.out.println("\nSet contents:\n");
while (i.hasNext()) {
System.out.println("Value: " + i.next());
}
// display the list
i = list.iterator();
System.out.println("\nList contents:\n");
while (i.hasNext()) {
System.out.println("Value: " + i.next());
}
}
}
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Oracle bean used for a few examples -->
<bean id="oracle" name="wiseworm" class="BookwormOracle"/>
<!-- collection injection samples -->
<bean id="injectCollection" class="CollectionInjection">
<property name="map">
<map>
<entry key="someValue">
<value>Hello World!</value>
</entry>
<entry key="someBean">
<ref local="oracle"/>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="firstName"> Rob </prop>
<prop key="secondName"> Harrop </prop>
</props>
</property>
<property name="set">
<set>
<value>Hello World!</value>
<ref local="oracle"/>
</set>
</property>
<property name="list">
<list>
<value>Hello World!</value>
<ref local="oracle"/>
</list>
</property>
</bean>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
public interface Oracle {
public String defineMeaningOfLife();
}
///////////////////////////////////////////////////////////////////////////////////////
public class Encyclopedia {
}
///////////////////////////////////////////////////////////////////////////////////////
public class BookwormOracle implements Oracle {
private Encyclopedia enc;
public void setEncyclopedia(Encyclopedia enc) {
this.enc = enc;
}
public String defineMeaningOfLife() {
return "Encyclopedia's are a waste of money - use the Internet";
}
}
///////////////////////////////////////////////////////////////////////////////////////
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class CollectionInjection {
private Map map;
private Properties props;
private Set set;
private List list;
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"));
CollectionInjection instance = (CollectionInjection) factory.getBean("injectCollection");
instance.displayInfo();
}
public void setList(List list) {
this.list = list;
}
public void setSet(Set set) {
this.set = set;
}
public void setMap(Map map) {
this.map = map;
}
public void setProps(Properties props) {
this.props = props;
}
public void displayInfo() {
// display the Map
Iterator i = map.keySet().iterator();
System.out.println("Map contents:\n");
while (i.hasNext()) {
Object key = i.next();
System.out.println("Key: " + key + " - Value: " + map.get(key));
}
// display the properties
i = props.keySet().iterator();
System.out.println("\nProperties contents:\n");
while (i.hasNext()) {
String key = i.next().toString();
System.out.println("Key: " + key + " - Value: "
+ props.getProperty(key));
}
// display the set
i = set.iterator();
System.out.println("\nSet contents:\n");
while (i.hasNext()) {
System.out.println("Value: " + i.next());
}
// display the list
i = list.iterator();
System.out.println("\nList contents:\n");
while (i.hasNext()) {
System.out.println("Value: " + i.next());
}
}
}
(出处:http://www.cncms.com)
Tags:Spring 配置 Collection
编辑录入:爽爽 [复制链接] [打 印]- ››配置MySQL出错The service could not be started....
- ››配置apache虚拟主机
- ››Spring源码学习-含有通配符路径解析(上)
- ››配置远程客户机使用命名管道协议访问SQL服务器
- ››配置SQL Server2005以允许远程访问
- ››配置ISA Server以创建站点到站点VPN 连接之一
- ››配置ISA Server以创建站点到站点VPN 连接之二
- ››配置ISA Server以创建站点到站点VPN 连接之三 启用...
- ››配置VPN服务器中的L2TP/IPSEC:ISA2006系列之二十...
- ››配置单网卡的ISA缓存服务器:ISA2006系列之二十七...
- ››配置Win2008作为远程访问SSL-VPN服务器之一
- ››配置Win2008作为远程访问SSL-VPN服务器之二
更多精彩
赞助商链接