1 16 17 package org.springframework.context.support; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 25 import org.springframework.beans.factory.config.PropertiesFactoryBean; 26 import org.springframework.context.ResourceLoaderAware; 27 import org.springframework.core.io.DefaultResourceLoader; 28 import org.springframework.core.io.Resource; 29 import org.springframework.core.io.ResourceLoader; 30 31 47 public class ResourceMapFactoryBean extends PropertiesFactoryBean implements ResourceLoaderAware { 48 49 private String resourceBasePath = ""; 50 51 private ResourceLoader resourceLoader = new DefaultResourceLoader(); 52 53 54 60 public void setResourceBasePath(String resourceBasePath) { 61 this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : ""); 62 } 63 64 public void setResourceLoader(ResourceLoader resourceLoader) { 65 this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); 66 } 67 68 69 public Class getObjectType() { 70 return Map .class; 71 } 72 73 76 protected Object createInstance() throws IOException { 77 Map resourceMap = new HashMap (); 78 Properties props = mergeProperties(); 79 for (Enumeration en = props.propertyNames(); en.hasMoreElements();) { 80 String key = (String ) en.nextElement(); 81 String location = props.getProperty(key); 82 resourceMap.put(key, getResource(location)); 83 } 84 return resourceMap; 85 } 86 87 94 protected Resource getResource(String location) { 95 return this.resourceLoader.getResource(this.resourceBasePath + location); 96 } 97 98 } 99 | Popular Tags |