Bean Injection Collection Properties : Properties Injection « Spring « Java Tutorial

File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean id="collectionsDemo" class="CollectionsDemo">
        <property name="map">
                <entry key="someValue">
                    <value>Hello World!</value>
                <entry key="someBean">
                    <ref local="oracle"/>
        <property name="props">
                <prop key="firstName">
                <prop key="secondName">
        <property name="set">
                <value>Hello World!</value>
                <ref local="oracle"/>
        <property name="list">
                <value>Hello World!</value>
                <ref local="oracle"/>

    <bean id="oracle" class="BookwormOracle"/>



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;

public class Main {

  public static void main(String[] args) {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    CollectionsDemo instance = (CollectionsDemo) factory.getBean("collectionsDemo");

interface Encyclopedia {
  Long findLong(String entry);

interface Oracle {
  String defineMeaningOfLife();

class BookwormOracle implements Oracle {
  private Encyclopedia encyclopedia;

  public String defineMeaningOfLife() {
    Long ageOfUniverse = this.encyclopedia.findLong("AgeOfUniverse");
    Long constantOfLife = this.encyclopedia.findLong("ConstantOfLife");
    return String.valueOf(ageOfUniverse / constantOfLife);

  public void setEncyclopedia(Encyclopedia encyclopedia) {
    this.encyclopedia = encyclopedia;

class CollectionsDemo {
  private Map map;

  private Properties props;

  private Set set;

  private List list;

  public void setList(List list) {
    this.list = list;

  public void setSet(Set set) {
    this.set = set;

  public void setMap(Map 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 =;
      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 =;
      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: " +;

    // display the list
    i = list.iterator();
    System.out.println("\nList contents:\n");
    while (i.hasNext()) {
      System.out.println("Value: " +;

  Download: 2,601 k)

28.5.Properties Injection
28.5.1.Fill Map
28.5.2.Fill Set
28.5.3.Fill Properties
28.5.4.Fill List To Another List
28.5.5.Fill Calendar Object To List
28.5.6.Properties Setting: Date
28.5.7.Property Setting Byte Array
28.5.8.Property Setting Boolean
28.5.9.Property Setting: BigDecimal
28.5.10.Property Setting Properties
28.5.11.Property Setting Integer
28.5.12.Property Setting File
28.5.13.Property Setting Class type
28.5.14.Property Setting Stream From URL
28.5.15.Property Setting: URL
28.5.16.Property Setting String Array
28.5.17.Fill Value To List
28.5.18.Create List, Map In Context
28.5.19.Bean Injection: Map
28.5.20.Bean Injection: Collection Set
28.5.21.Bean Injection Collection Properties
28.5.22.Bean Injection: Collection Map
28.5.23.Bean Injection: Collection List
28.5.24.Load property File In Context