1. package org.springframework.context.support;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Properties;
  6. import org.springframework.beans.factory.config.PropertiesFactoryBean;
  7. import org.springframework.context.ResourceLoaderAware;
  8. import org.springframework.core.io.DefaultResourceLoader;
  9. import org.springframework.core.io.ResourceLoader;
  10. /**
  11. * FactoryBean that creates a Map with String keys and Resource values from
  12. * properties, interpreting passed-in String values as resource locations.
  13. *
  14. * <p>Extends PropertiesFactoryBean to inherit the capability of defining
  15. * local properties and loading from properties files.
  16. *
  17. * <p>Implements the ResourceLoaderAware interface to automatically use
  18. * the context ResourceLoader if running in an ApplicationContext.
  19. * Uses DefaultResourceLoader else.
  20. *
  21. * @author Juergen Hoeller
  22. * @author Keith Donald
  23. * @since 25.04.2004
  24. * @see org.springframework.core.io.DefaultResourceLoader
  25. */
  26. public class ResourceMapFactoryBean extends PropertiesFactoryBean implements ResourceLoaderAware {
  27. private String resourceBasePath = "";
  28. private ResourceLoader resourceLoader = new DefaultResourceLoader();
  29. /**
  30. * Set a base path to prepend to each resource location value
  31. * in the properties file.
  32. * <p>E.g.: resourceBasePath="/images", value="/test.gif"
  33. * -> location="/images/test.gif"
  34. */
  35. public void setResourceBasePath(String resourceBasePath) {
  36. this.resourceBasePath = resourceBasePath;
  37. }
  38. public void setResourceLoader(ResourceLoader resourceLoader) {
  39. this.resourceLoader = resourceLoader;
  40. }
  41. public Class getObjectType() {
  42. return Map.class;
  43. }
  44. protected Object createInstance() throws Exception {
  45. Map resourceMap = new HashMap();
  46. Properties props = mergeProperties();
  47. for (Iterator it = props.keySet().iterator(); it.hasNext();) {
  48. String key = (String) it.next();
  49. String location = props.getProperty(key);
  50. resourceMap.put(key, this.resourceLoader.getResource(this.resourceBasePath + location));
  51. }
  52. return resourceMap;
  53. }
  54. }