1 16 17 package org.springframework.core.io; 18 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 22 import org.springframework.util.Assert; 23 import org.springframework.util.ClassUtils; 24 25 40 public class DefaultResourceLoader implements ResourceLoader { 41 42 private ClassLoader classLoader; 43 44 45 51 public DefaultResourceLoader() { 52 this.classLoader = ClassUtils.getDefaultClassLoader(); 53 } 54 55 60 public DefaultResourceLoader(ClassLoader classLoader) { 61 this.classLoader = classLoader; 62 } 63 64 65 71 public void setClassLoader(ClassLoader classLoader) { 72 this.classLoader = classLoader; 73 } 74 75 83 public ClassLoader getClassLoader() { 84 return this.classLoader; 85 } 86 87 88 public Resource getResource(String location) { 89 Assert.notNull(location, "Location must not be null"); 90 if (location.startsWith(CLASSPATH_URL_PREFIX)) { 91 return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); 92 } 93 else { 94 try { 95 URL url = new URL (location); 97 return new UrlResource(url); 98 } 99 catch (MalformedURLException ex) { 100 return getResourceByPath(location); 102 } 103 } 104 } 105 106 117 protected Resource getResourceByPath(String path) { 118 return new ClassPathResource(path, getClassLoader()); 119 } 120 121 } 122 | Popular Tags |