Create List, Map In Context : Properties Injection « Spring « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Spring » Properties Injection 
28.5.18.Create List, Map In ContextPrevious/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"
       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>

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.Entryo;
      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[] argsthrows Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/context.xml");

    List y = (Listcontext.getBean("Y");
    printList(y);

    Map z = (Mapcontext.getBean("Z");
    printMap(z);

    Map p = (Mapcontext.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
  Download:  Spring-CreateUtilListMapInContext.zip( 4,651 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.