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






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[] args) throws 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);

}
  Download:  Spring-BeanInjectionMap.zip( 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