Java/Spring/XML Bean Property — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:16, 1 июня 2010

Bean Injection Collection: List

       
File: Main.java
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.ClassPathResource;
public class Main {
  public static void main(String[] args) {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    CollectionsDemo instance = (CollectionsDemo) factory.getBean("collectionsDemo");
    instance.displayInfo();
  }
}
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) {
    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: context.xml
<?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" class="CollectionsDemo">
        <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">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </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>
    <bean id="oracle" class="BookwormOracle"/>
</beans>





Bean Injection Collection: Map

       
File: context.xml
<?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" class="CollectionsDemo">
        <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">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </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>
    <bean id="oracle" class="BookwormOracle"/>
</beans>

File: Main.java
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.ClassPathResource;
public class Main {
  public static void main(String[] args) {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    CollectionsDemo instance = (CollectionsDemo) factory.getBean("collectionsDemo");
    instance.displayInfo();
  }
}
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) {
    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());
    }
  }
}





Bean Injection: Collection Properties

File: Main.java
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.ClassPathResource;
public class Main {
  public static void main(String[] args) {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    CollectionsDemo instance = (CollectionsDemo) factory.getBean("collectionsDemo");
    instance.displayInfo();
  }
}
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) {
    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: context.xml
<?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" class="CollectionsDemo">
        <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">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </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>
    <bean id="oracle" class="BookwormOracle"/>
</beans>





Bean Injection Collection: Set

       
File: context.xml
<?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" class="CollectionsDemo">
        <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">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </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>
    <bean id="oracle" class="BookwormOracle"/>
</beans>

File: Main.java
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.ClassPathResource;
public class Main {
  public static void main(String[] args) {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    CollectionsDemo instance = (CollectionsDemo) factory.getBean("collectionsDemo");
    instance.displayInfo();
  }
}
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) {
    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());
    }
  }
}





Bean Injection: Map

       
File: context.xml
<?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="ftpDownloader" class="FtpDownloader"/>
    <bean id="httpDownloader" class="HttpDownloader"/>
    <bean id="sftpDownaloader" class="SftpDownloader"/>
    <bean id="downloadManager" class="DownloadManager">
        <property name="downloaders">
            <map>
                <entry key="ftp" value-ref="ftpDownloader"/>
                <entry key="http" value-ref="httpDownloader"/>
                <entry key="sftp" value-ref="sftpDownaloader"/>
            </map>
        </property>
    </bean>

</beans>

File: Main.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.ruponent;
public class Main {
  public static void main(String[] args) throws Exception {
    XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("context.xml"));
    DownloadManager dm = (DownloadManager)xbf.getBean("downloadManager");
    String uri = "http://www.jexp.ru";
    dm.download(uri);
    
  }
}
class DownloadManager {
  private static final Pattern URI_PATTERN = Pattern.rupile("^([^:]*):/(.*)$");
  private Map<String, Downloader> downloaders;

  void download(String uri) {
      Matcher matcher = URI_PATTERN.matcher(uri);
      if (matcher.matches()) {
          String scheme = matcher.group(1);
          String path = matcher.group(2);
          Downloader downloader = this.downloaders.get(scheme);
          if (downloader != null) {
              downloader.download(path);
          }
      }
  }
  public void setDownloaders(Map<String, Downloader> downloaders) {
      this.downloaders = downloaders;
  }
}
@Component
class SftpDownloader implements Downloader {
    public byte[] download(String uri) {
        System.out.println("SFTP Downloading " + uri);
        return new byte[0];
    }
}
@Component
 class HttpDownloader implements Downloader {
    public byte[] download(String uri) {
        System.out.println("HTTP Downloading " + uri);
        return new byte[0];
    }
}
@Component
 class FtpDownloader implements Downloader {
    public byte[] download(String uri) {
        System.out.println("FTP Downloading " + uri);
        return new byte[0];
    }
}
interface Downloader {
    byte[] download(final String uri);
}





Create List Map In Context

       
File: Main.java
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;
public class Main {
  private static void printMap(Map z) {
    for (Object o : z.entrySet()) {
      Map.Entry e = (Map.Entry) o;
      System.out.println(e.getKey() + " => " + e.getValue().getClass() + " " + e.getValue());
    }
  }
  private static void printList(List y) {
    for (Object o : y) {
      System.out.println(o.getClass() + " " + o);
    }
  }
  public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/context.xml");
    List y = (List) context.getBean("Y");
    printList(y);
    Map z = (Map) context.getBean("Z");
    printMap(z);
    Map p = (Map) context.getBean("P");
    printMap(p);
  }
}
class MyTransactionManager extends AbstractPlatformTransactionManager {
  @Override
  protected Object doGetTransaction() throws TransactionException {
    System.out.println("doGetTransaction");
    return new Object();
  }
  @Override
  protected void doBegin(Object object, TransactionDefinition transactionDefinition)
      throws TransactionException {
    System.out.println("doBegin");
  }
  @Override
  protected void doCommit(DefaultTransactionStatus defaultTransactionStatus)
      throws TransactionException {
    System.out.println("doCommit");
  }
  @Override
  protected void doRollback(DefaultTransactionStatus defaultTransactionStatus)
      throws TransactionException {
    System.out.println("doRollback");
  }
}
class SimpleBean {
  private String name;
  private String value;
  public SimpleBean() {
    this.name = "My name";
    this.value = "My value";
  }
  public String getName() {
    return name;
  }
  public String getValue() {
    return value;
  }
}

File: Main.properties
foo=bar
baz=foobar

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <bean id="transactionManager" class="MyTransactionManager"/>
    <util:constant id="X" static-field="java.lang.Integer.MAX_VALUE"/>
    <util:list id="Y" list-class="java.util.ArrayList">
        <value>value1</value>
        <ref bean="X"/>
    </util:list>
    <util:list id="greetingsList">
        <value>Hello, world</value>
        <value>How are you doing today?</value>
    </util:list>
    <util:map id="Z" map-class="java.util.HashMap">
        <entry key="x" value="y"/>
        <entry key="y"><ref bean="X"/></entry>
    </util:map>
    <util:properties id="P" location="classpath:Main.properties"/>
    <bean id="simple" class="SimpleBean"/>
    <util:property-path id="Q" path="simple.name"/>
    <util:set id="S" set-class="java.util.HashSet">
        <value>foo</value>
        <ref bean="X"/>
    </util:set>
</beans>





Fill Calendar Object To List

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theList">
      <list>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
        <ref local="curDate"/>
        
      </list>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>

File: Main.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    
    
    CollectionsBean example = (CollectionsBean) ctx.getBean("collectionsExample");
   System.out.println(example.getTheList());
  
  }
}
class CollectionsBean {
  
  private List theList;
  private Set theSet;
  private Map theMap;
  private Properties theProperties;
  
  public void setTheList(List theList) {
    this.theList = theList;
  }
  public List getTheList() {
    return theList;
  }
  
  public void setTheSet(Set theSet) {
    this.theSet = theSet;
  }
  public Set getTheSet() {
    return theSet;
  }
  
  public void setTheMap(Map theMap) {
    this.theMap = theMap;
  }
  public Map getTheMap() {
    return theMap;
  }
  
  public void setTheProperties(Properties theProperties) {
    this.theProperties = theProperties;
  }
  public Properties getTheProperties() {
    return theProperties;
  }
}





Fill List To Another List

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theList">
      <list>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
        <list>
          <value>one</value>
          <value>two</value>
          <value>three</value>
        </list>
        
      </list>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>

File: Main.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    
    
    CollectionsBean example = (CollectionsBean) ctx.getBean("collectionsExample");
   System.out.println(example.getTheList());
  
  }
}
class CollectionsBean {
  
  private List theList;
  private Set theSet;
  private Map theMap;
  private Properties theProperties;
  
  public void setTheList(List theList) {
    this.theList = theList;
  }
  public List getTheList() {
    return theList;
  }
  
  public void setTheSet(Set theSet) {
    this.theSet = theSet;
  }
  public Set getTheSet() {
    return theSet;
  }
  
  public void setTheMap(Map theMap) {
    this.theMap = theMap;
  }
  public Map getTheMap() {
    return theMap;
  }
  
  public void setTheProperties(Properties theProperties) {
    this.theProperties = theProperties;
  }
  public Properties getTheProperties() {
    return theProperties;
  }
}





Fill Map

       
File: Main.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    
    
    CollectionsBean example = (CollectionsBean) ctx.getBean("collectionsExample");
   System.out.println(example.getTheMap());
  
  }
}
class CollectionsBean {
  
  private List theList;
  private Set theSet;
  private Map theMap;
  private Properties theProperties;
  
  public void setTheList(List theList) {
    this.theList = theList;
  }
  public List getTheList() {
    return theList;
  }
  
  public void setTheSet(Set theSet) {
    this.theSet = theSet;
  }
  public Set getTheSet() {
    return theSet;
  }
  
  public void setTheMap(Map theMap) {
    this.theMap = theMap;
  }
  public Map getTheMap() {
    return theMap;
  }
  
  public void setTheProperties(Properties theProperties) {
    this.theProperties = theProperties;
  }
  public Properties getTheProperties() {
    return theProperties;
  }
}

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theMap">
      <map>
        <entry key="left">
          <value>right</value>
        </entry>
        <entry key="up">
          <value>down</value>
        </entry>
        <entry key="date">
          <ref local="curDate"/>
        </entry>
      </map>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>





Fill Properties

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    

    <property name="theProperties">
      <props>
        <prop key="left">right</prop>
        <prop key="up">down</prop>
      </props>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>

File: Main.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    
    
    CollectionsBean example = (CollectionsBean) ctx.getBean("collectionsExample");
   System.out.println(example.getTheProperties());
  
  }
}
class CollectionsBean {
  
  private List theList;
  private Set theSet;
  private Map theMap;
  private Properties theProperties;
  
  public void setTheList(List theList) {
    this.theList = theList;
  }
  public List getTheList() {
    return theList;
  }
  
  public void setTheSet(Set theSet) {
    this.theSet = theSet;
  }
  public Set getTheSet() {
    return theSet;
  }
  
  public void setTheMap(Map theMap) {
    this.theMap = theMap;
  }
  public Map getTheMap() {
    return theMap;
  }
  
  public void setTheProperties(Properties theProperties) {
    this.theProperties = theProperties;
  }
  public Properties getTheProperties() {
    return theProperties;
  }
}





Fill Set

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theSet">
      <set>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
      </set>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>

File: Main.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    
    
    CollectionsBean example = (CollectionsBean) ctx.getBean("collectionsExample");
   System.out.println(example.getTheSet());
  
  }
}
class CollectionsBean {
  
  private List theList;
  private Set theSet;
  private Map theMap;
  private Properties theProperties;
  
  public void setTheList(List theList) {
    this.theList = theList;
  }
  public List getTheList() {
    return theList;
  }
  
  public void setTheSet(Set theSet) {
    this.theSet = theSet;
  }
  public Set getTheSet() {
    return theSet;
  }
  
  public void setTheMap(Map theMap) {
    this.theMap = theMap;
  }
  public Map getTheMap() {
    return theMap;
  }
  
  public void setTheProperties(Properties theProperties) {
    this.theProperties = theProperties;
  }
  public Properties getTheProperties() {
    return theProperties;
  }
}





Fill Value To List

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theList">
      <list>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
      </list>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>

File: Main.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    
    
    CollectionsBean example = (CollectionsBean) ctx.getBean("collectionsExample");
   System.out.println(example.getTheList());
  
  }
}
class CollectionsBean {
  
  private List theList;
  private Set theSet;
  private Map theMap;
  private Properties theProperties;
  
  public void setTheList(List theList) {
    this.theList = theList;
  }
  public List getTheList() {
    return theList;
  }
  
  public void setTheSet(Set theSet) {
    this.theSet = theSet;
  }
  public Set getTheSet() {
    return theSet;
  }
  
  public void setTheMap(Map theMap) {
    this.theMap = theMap;
  }
  public Map getTheMap() {
    return theMap;
  }
  
  public void setTheProperties(Properties theProperties) {
    this.theProperties = theProperties;
  }
  public Properties getTheProperties() {
    return theProperties;
  }
}





lazy init

       
File: context.xml
<?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="destructiveBean" lazy-init="false" class="java.lang.String">
         
    </bean>
</beans>

File: Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    ctx.getBean("destructiveBean");
  }
}





Load property File In Context

       
File: Main.java
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;
public class Main {
  private static void printMap(Map z) {
    for (Object o : z.entrySet()) {
      Map.Entry e = (Map.Entry) o;
      System.out.println(e.getKey() + " => " + e.getValue().getClass() + " " + e.getValue());
    }
  }
  private static void printList(List y) {
    for (Object o : y) {
      System.out.println(o.getClass() + " " + o);
    }
  }
  public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/context.xml");
    List y = (List) context.getBean("Y");
    printList(y);
    Map z = (Map) context.getBean("Z");
    printMap(z);
    Map p = (Map) context.getBean("P");
    printMap(p);
  }
}
class MyTransactionManager extends AbstractPlatformTransactionManager {
  @Override
  protected Object doGetTransaction() throws TransactionException {
    System.out.println("doGetTransaction");
    return new Object();
  }
  @Override
  protected void doBegin(Object object, TransactionDefinition transactionDefinition)
      throws TransactionException {
    System.out.println("doBegin");
  }
  @Override
  protected void doCommit(DefaultTransactionStatus defaultTransactionStatus)
      throws TransactionException {
    System.out.println("doCommit");
  }
  @Override
  protected void doRollback(DefaultTransactionStatus defaultTransactionStatus)
      throws TransactionException {
    System.out.println("doRollback");
  }
}
class SimpleBean {
  private String name;
  private String value;
  public SimpleBean() {
    this.name = "My name";
    this.value = "My value";
  }
  public String getName() {
    return name;
  }
  public String getValue() {
    return value;
  }
}

File: Main.properties
foo=bar
baz=foobar

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <bean id="transactionManager" class="MyTransactionManager"/>
    <util:constant id="X" static-field="java.lang.Integer.MAX_VALUE"/>
    <util:list id="Y" list-class="java.util.ArrayList">
        <value>value1</value>
        <ref bean="X"/>
    </util:list>
    <util:list id="greetingsList">
        <value>Hello, world</value>
        <value>How are you doing today?</value>
    </util:list>
    <util:map id="Z" map-class="java.util.HashMap">
        <entry key="x" value="y"/>
        <entry key="y"><ref bean="X"/></entry>
    </util:map>
    <util:properties id="P" location="classpath:Main.properties"/>
    <bean id="simple" class="SimpleBean"/>
    <util:property-path id="Q" path="simple.name"/>
    <util:set id="S" set-class="java.util.HashSet">
        <value>foo</value>
        <ref bean="X"/>
    </util:set>
</beans>





Properties Setting: Date

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  
  <bean id="customEditorConfigurer"
        class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
      <map>
        <entry key="java.util.Date">
          <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
            <constructor-arg index="0">
              <bean class="java.text.SimpleDateFormat">
                <constructor-arg><value>M/d/yy</value></constructor-arg>
              </bean>
            </constructor-arg>
            <constructor-arg index="1"><value>true</value></constructor-arg>
          </bean>
        </entry>
        
      </map>
    </property>
  </bean>
  <bean id="startEndDatesBean" class="StartEndDatesBean">
    <property name="startDate"><value>10/09/2001</value></property>
    <property name="endDate"><value>10/26/2008</value></property>
  </bean>
</beans>

File: Main.java
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "context.xml");
    
    StartEndDatesBean startEnd = (StartEndDatesBean) ctx.getBean("startEndDatesBean");
  }
}
class StartEndDatesBean {
  
  Date startDate;
  Date endDate;
  
  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }
  public Date getStartDate() {
    return startDate;
  }
  
  public void setEndDate(Date endDate) {
    this.endDate = endDate;
  }
  public Date getEndDate() {
    return endDate;
  }
}





Property Setting: BigDecimal

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myAmount" value="1000000"/>
  </bean>
</beans>

File: Main.java
import java.math.BigDecimal;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyAmount());
  }
}
class PropertyEditorTestBean {
  private BigDecimal myAmount;
  public BigDecimal getMyAmount() {
    return myAmount;
  }
  public void setMyAmount(BigDecimal myAmount) {
    this.myAmount = myAmount;
  }
}





Property Setting: Boolean

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myToggle" value="false"/>
  </bean>
</beans>

File: Main.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.isMyToggle());
  }
}
class PropertyEditorTestBean {
  private boolean myToggle;
  public boolean isMyToggle() {
      return myToggle;
  }
  public void setMyToggle(boolean myToggle) {
      this.myToggle = myToggle;
  }
}





Property Setting: Byte Array

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myBytes" value="some bytes"/>
  </bean>
</beans>

File: Main.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyBytes().length);
  }
}
class PropertyEditorTestBean {
  private byte[] myBytes;
  public byte[] getMyBytes() {
    return myBytes;
  }
  public void setMyBytes(byte[] myBytes) {
    this.myBytes = myBytes;
  }
}





Property Setting: Class type

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myClass" value="java.util.Collection"/>
  </bean>
</beans>

File: Main.java
import java.math.BigDecimal;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyClass());
  }
}
class PropertyEditorTestBean {
  private Class myClass;
  public Class getMyClass() {
      return myClass;
  }
  public void setMyClass(Class myClass) {
      this.myClass = myClass;
  }
}





Property Setting: File

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myFile" value="placeholder.txt"/>
  </bean>
</beans>

File: Main.java
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyFile());
  }
}
class PropertyEditorTestBean {
  private File myFile;
  public File getMyFile() {
    return myFile;
  }
  public void setMyFile(File myFile) {
    this.myFile = myFile;
  }
}





Property Setting: Integer

       
File: Main.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyNumber());
  }
}
class PropertyEditorTestBean {
  private int myNumber;
  public int getMyNumber() {
    return myNumber;
  }
  public void setMyNumber(int myNumber) {
    this.myNumber = myNumber;
  }
}

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myNumber" value="500"/>
  </bean>
</beans>





Property Setting: Properties

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
        <property name="myProperties">
          <value>
          firstname=f
          lastname=l
          </value>
        </property>
  </bean>
</beans>

File: Main.java
import java.util.Properties;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyProperties());
  }
}
class PropertyEditorTestBean {
  private Properties myProperties;
  public Properties getMyProperties() {
    return myProperties;
  }
  public void setMyProperties(Properties myProperties) {
    this.myProperties = myProperties;
  }
}





Property Setting: Stream From URL

       
       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myInputStream" value="http://www.jexp.ru"/>
  </bean>
</beans>

File: Main.java
import java.io.InputStream;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyInputStream());
  }
}
class PropertyEditorTestBean {
  private InputStream myInputStream;
  public InputStream getMyInputStream() {
    return myInputStream;
  }
  public void setMyInputStream(InputStream myInputStream) {
    this.myInputStream = myInputStream;
  }
}





PropertySetting: String Array

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myStrings" value="A,B,C,D"/>
  </bean>
</beans>

File: Main.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyStrings().length);
  }
}
class PropertyEditorTestBean {
  private String[] myStrings;
  public String[] getMyStrings() {
    return myStrings;
  }
  public void setMyStrings(String[] myStrings) {
    this.myStrings = myStrings;
  }
}





Property Setting: URL

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myUrl" value="http://www.jexp.ru"/>
  </bean>
</beans>

File: Main.java
import java.net.URL;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyUrl());
  }
}
class PropertyEditorTestBean {
  private URL myUrl;
  public URL getMyUrl() {
    return myUrl;
  }
  public void setMyUrl(URL myUrl) {
    this.myUrl = myUrl;
  }
}