Bean Injection: Map : XML Bean Property « Spring « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Spring » XML Bean Property 




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.Component;

public class Main {

  public static void main(String[] argsthrows Exception {
    XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("context.xml"));
    DownloadManager dm = (DownloadManager)xbf.getBean("downloadManager");
    String uri = "http://www.java2s.com";
    dm.download(uri);
    
  }
}
class DownloadManager {
  private static final Pattern URI_PATTERN = Pattern.compile("^([^:]*):/(.*)$");
  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);

}




           
       














Spring-BeanInjectionMap.zip( 2,601 k)
Related examples in the same category
1.Property Setting: URL
2.PropertySetting: String Array
3.Property Setting: Stream From URL
4.Property Setting: Properties
5.Property Setting: Integer
6.Property Setting: File
7.Property Setting: Class type
8.Property Setting: Byte Array
9.Property Setting: Boolean
10.Property Setting: BigDecimal
11.Properties Setting: Date
12.Load property File In Context
13.lazy init
14.Fill Value To List
15.Fill Map
16.Fill Set
17.Fill Properties
18.Fill List To Another List
19.Fill Calendar Object To List
20.Create List Map In Context
21.Bean Injection Collection: Set
22.Bean Injection: Collection Properties
23.Bean Injection Collection: Map
24.Bean Injection Collection: List
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.