相关源码注释
ApplicationContext
Spring 5 DefaultResourceLoader 源码注释 Spring 5 AbstractApplicationContext 源码注释
BeanFactory
Spring 5 SimpleAliasRegistry 源码注释 Spring 5 DefaultSingletonBeanRegistry 源码注释 Spring 5 FactoryBeanRegistrySupport 源码注释 Spring 5 AbstractBeanFactory 源码注释 Spring 5 AbstractAutowireCapableBeanFactory 源码注释 Spring 5 DefaultLisbaleBeanFactory 源码注释
UML类图
源码
public class DefaultResourceLoader implements ResourceLoader {
@Nullable
private ClassLoader classLoader
;
private final Set
<ProtocolResolver> protocolResolvers
= new LinkedHashSet<>(4);
private final Map
<Class
<?>, Map
<Resource
, ?>> resourceCaches
= new ConcurrentHashMap<>(4);
public DefaultResourceLoader() {
this.classLoader
= ClassUtils
.getDefaultClassLoader();
}
public DefaultResourceLoader(@Nullable ClassLoader classLoader
) {
this.classLoader
= classLoader
;
}
public void setClassLoader(@Nullable ClassLoader classLoader
) {
this.classLoader
= classLoader
;
}
@Override
@Nullable
public ClassLoader
getClassLoader() {
return (this.classLoader
!= null
? this.classLoader
: ClassUtils
.getDefaultClassLoader());
}
public void addProtocolResolver(ProtocolResolver resolver
) {
Assert
.notNull(resolver
, "ProtocolResolver must not be null");
this.protocolResolvers
.add(resolver
);
}
public Collection
<ProtocolResolver> getProtocolResolvers() {
return this.protocolResolvers
;
}
@SuppressWarnings("unchecked")
public <T> Map
<Resource, T> getResourceCache(Class
<T> valueType
) {
return (Map
<Resource, T>) this.resourceCaches
.computeIfAbsent(valueType
, key
-> new ConcurrentHashMap<>());
}
public void clearResourceCaches() {
this.resourceCaches
.clear();
}
@Override
public Resource
getResource(String location
) {
Assert
.notNull(location
, "Location must not be null");
for (ProtocolResolver protocolResolver
: getProtocolResolvers()) {
Resource resource
= protocolResolver
.resolve(location
, this);
if (resource
!= null
) {
return resource
;
}
}
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 {
URL url
= new URL(location
);
return (ResourceUtils
.isFileURL(url
) ? new FileUrlResource(url
) : new UrlResource(url
));
}
catch (MalformedURLException ex
) {
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
, @Nullable 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());
}
}
}
ClassPathResource
Spring 5 Resource 源码注释