每日一则JavaWeb---Spring的ResourceLoader的作用
2017-08-30 18:01
771 查看
先来看看的源码
非常简单就两个功能,一个是获取Resource,一个是获取ClassLoader
看看默认实现又有啥,在Spring的默认套路中除了一些标志性的接口如aware接口,一般abstract和interface都会有一个DefaultXXX的默认实现。
DefaultResourceLoader怎么实现的
看着实现其实没有太多可以讲的,可以看看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;
}
其中这个接口对ResourceLoader进行了扩展,但是扩展也就是扩展 了通过多个文件的情形。。。
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; }
相关文章推荐
- 每日一则JavaWeb---Spring的OncePerRequestFilter的作用
- 每日一则JavaWeb---Spring的MessageSource的作用
- 每日一则JavaWeb---Spring的ApplicationEventPublisher的作用
- 每日一则JavaWeb---spring 各个包的作用
- 每日一则JavaWeb---spring的<util:> 和<p:>
- 每日一则JavaWeb---Spring之ContextLoaderListener
- 每日一则JavaWeb---spring中非Controller层使用HttpServletRequest两种解决方案
- 每日一则JavaWeb---spring的ApplicationContext
- JavaWeb-Spring中各个jar包的作用
- 每日一则JavaWeb---实验分析Servlet、Filter 和 Listener 调用顺序、生命周期
- 每日一则JavaWeb---ClassLoader原理解析
- 每日一则JavaWeb---SpringMVC的HiddenHttpMethodFilter过滤器
- 2017-06-21 web.xml中的contextConfigLocation在spring中的作用
- 解决在tomecat启动时报 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListen
- Java Web中hibernate、spring、structs简要说明
- springMVC+redis+mybatis的javaWeb框架的搭建---套路实战(1)
- Javaweb 实战之Spring(二)
- JavaWeb开发基于Spring和mybatis框架(学习日志)
- maven项目启动报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener