3-28集合注入
Bean:
package com.apress.prospring2.ch03.beanfactory;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import java.util.*;/** * @author janm */public class CollectionsDemo { private Map map; private Properties props; private Set set; private List list; public static void main(String[] args) { BeanFactory factory = new XmlBeanFactory( new ClassPathResource("/META-INF/spring/collectionsdemo1-context.xml")); CollectionsDemo instance = (CollectionsDemo)factory.getBean("collectionsDemo"); 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()); } }}<?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="collectionsDemo" name="code">Map contents:Key: someValue - Value: Hello World!Key: someBean - Value: com.apress.prospring2.ch03.di.BookwormOracle@ef5502Properties contents:Key: secondName - Value: MachacekKey: firstName - Value: JanSet contents:Value: Hello World!Value: com.apress.prospring2.ch03.di.BookwormOracle@ef5502List contents:Value: Hello World!Value: com.apress.prospring2.ch03.di.BookwormOracle@ef5502