您的位置:首页 > 编程语言 > Java开发

每日一则JavaWeb---Spring的ResourceLoader的作用

2017-08-30 18:01 771 查看
先来看看的源码

public interface ResourceLoader {

/** Pseudo URL prefix for loading from the class path: "classpath:" */
String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;

Resource getResource(String location);

ClassLoader getClassLoader();

}


非常简单就两个功能,一个是获取Resource,一个是获取ClassLoader

看看默认实现又有啥,在Spring的默认套路中除了一些标志性的接口如aware接口,一般abstract和interface都会有一个DefaultXXX的默认实现。

DefaultResourceLoader怎么实现的

public class DefaultResourceLoader implements ResourceLoader {

private ClassLoader classLoader;

public DefaultResourceLoader() {
this.classLoader = ClassUtils.getDefaultClassLoader();
}

public DefaultResourceLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

@Override
public ClassLoader getClassLoader() {
return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
}

@Override
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
if (location.startsWith("/")) {
return getResourceByPath(location);
}
else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
}
else {
try {
// Try to parse the location as a URL...
URL url = new URL(location);
return new UrlResource(url);
}
catch (MalformedURLException ex) {
// No URL -> resolve as resource path.
return getResourceByPath(location);
}
}
}

protected Resource getResourceByPath(String path) {
return new ClassPathContextResource(path, getClassLoader());
}

protected static class ClassPathContextResource extends ClassPathResource implements ContextResource {

public ClassPathContextResource(String path, ClassLoader classLoader) {
super(path, classLoader);
}

@Override
public String getPathWithinContext() {
return getPath();
}

@Override
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
return new ClassPathContextResource(pathToUse, getClassLoader());
}
}

}

看着实现其实没有太多可以讲的,可以看看ClassLoader的加载的过程

public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return cl;
}
ResourcePatternResolver

其中这个接口对ResourceLoader进行了扩展,但是扩展也就是扩展 了通过多个文件的情形。。。

String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;

String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

public interface ResourcePatternResolver extends ResourceLoader {

String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

Resource[] getResources(String locationPattern) throws IOException;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: