ResourceLoader模块学习研究

/** @author Juergen Hoeller
 * @since 10.03.2004
 * @see Resource
 * @see org.springframework.core.io.support.ResourcePatternResolver
 * @see org.springframework.context.ApplicationContext
 * @see org.springframework.context.ResourceLoaderAware
 */
public interface ResourceLoader {
 /** Pseudo URL prefix for loading from the class path: "classpath:" */
 String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
 /**
  * Return a Resource handle for the specified resource.
  * The handle should always be a reusable resource descriptor,
  * allowing for multiple {@link Resource#getInputStream()} calls.
  * <p><ul>
  * <li>Must support fully qualified URLs, e.g. "file:C:/test.dat".
  * <li>Must support classpath pseudo-URLs, e.g. "classpath:test.dat".
  * <li>Should support relative file paths, e.g. "WEB-INF/test.dat".
  * (This will be implementation-specific, typically provided by an
  * ApplicationContext implementation.)
  * </ul>
  * <p>Note that a Resource handle does not imply an existing resource;
  * you need to invoke {@link Resource#exists} to check for existence.
  * @param location the resource location
  * @return a corresponding Resource handle
  * @see #CLASSPATH_URL_PREFIX
  * @see org.springframework.core.io.Resource#exists
  * @see org.springframework.core.io.Resource#getInputStream
  */
 Resource getResource(String location);
 /**
  * Expose the ClassLoader used by this ResourceLoader.
  * <p>Clients which need to access the ClassLoader directly can do so
  * in a uniform manner with the ResourceLoader, rather than relying
  * on the thread context ClassLoader.
  * @return the ClassLoader (never <code>null</code>)
  */
 ClassLoader getClassLoader();
}

一个属性:

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

这个方法制造了了一个假的class path 路径,也就是"classpath:"

第一个方法:

 /**
  * Return a Resource handle for the specified resource.
  * The handle should always be a reusable resource descriptor,
  * allowing for multiple  Resource#getInputStream()} calls.
  * <p><ul>
  * <li>Must support fully qualified URLs, e.g. "file:C:/test.dat".
  * <li>Must support classpath pseudo-URLs, e.g. "classpath:test.dat".
  * <li>Should support relative file paths, e.g. "WEB-INF/test.dat".
  * (This will be implementation-specific, typically provided by an
  * ApplicationContext implementation.)
  * </ul>
  * <p>Note that a Resource handle does not imply an existing resource;
  * you need to invoke  Resource#exists} to check for existence.
  * @param location the resource location
  * @return a corresponding Resource handle
  * @see #CLASSPATH_URL_PREFIX
  * @see org.springframework.core.io.Resource#exists
  * @see org.springframework.core.io.Resource#getInputStream
  */
 Resource getResource(String location);

这个方法的注释意思是“返回特定资源的句柄”,这个句柄通常是一个可重用的资源描述符,允许Resource的实现类的getInputStream方法多次调用,这个句柄支持三类的资源描述符,分别是,完整路径,带有假前缀的路径这两个必须支持,应该支持相对路径,就是网络容器的路径。方法会在ApplicationContext的实现类中进行典型的实现。需要注意这个handle并不确定资源已经存在,需要调用Resource中的exists方法对存在性进行检查。

第二个方法

  /**
  * Expose the ClassLoader used by this ResourceLoader.
  * <p>Clients which need to access the ClassLoader directly can do so
  * in a uniform manner with the ResourceLoader, rather than relying
  * on the thread context ClassLoader.
  * @return the ClassLoader (never <code>null</code>)
  */
 ClassLoader getClassLoader();

现实这个ResourceLoader中的ClassLoder,客户需要此功能可以统一调用此方法,而不需借助线程上下文加载器。

 

-------------------------

/** @author Juergen Hoeller
 * @since 1.0.2
 * @see org.springframework.core.io.Resource
 * @see org.springframework.core.io.ResourceLoader
 * @see org.springframework.context.ApplicationContext
 * @see org.springframework.context.ResourceLoaderAware
 */
public interface ResourcePatternResolver extends ResourceLoader {
 /**
  * Pseudo URL prefix for all matching resources from the class path: "classpath*:"
  * This differs from ResourceLoader's classpath URL prefix in that it
  * retrieves all matching resources for a given name (e.g. "/beans.xml"),
  * for example in the root of all deployed JAR files.
  * @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX
  */
 String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
 /**
  * Resolve the given location pattern into Resource objects.
  * <p>Overlapping resource entries that point to the same physical
  * resource should be avoided, as far as possible. The result should
  * have set semantics.
  * @param locationPattern the location pattern to resolve
  * @return the corresponding Resource objects
  * @throws IOException in case of I/O errors
  */
 Resource[] getResources(String locationPattern) throws IOException;
}

ResourcePatternResolver接口扩展了ResourceLoader的功能:

 /**
  * Pseudo URL prefix for all matching resources from the class path: "classpath*:"
  * This differs from ResourceLoader's classpath URL prefix in that it
  * retrieves all matching resources for a given name (e.g. "/beans.xml"),
  * for example in the root of all deployed JAR files.
  * @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX
  */
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

这个方法是用通配符,使此前缀能通配目录下的所有匹配文件,李荣在部署JAR files的根目录中会匹配根目录下所有匹配的文件。

/**
  * Resolve the given location pattern into Resource objects.
  * <p>Overlapping resource entries that point to the same physical
  * resource should be avoided, as far as possible. The result should
  * have set semantics.
  * @param locationPattern the location pattern to resolve
  * @return the corresponding Resource objects
  * @throws IOException in case of I/O errors
  */
 Resource[] getResources(String locationPattern) throws IOException;

返回一个或者多个Resource资源,应尽量避免对指向相同物力资源的覆盖动作,结果应该已经被设置该有的语义。

 

--------

对ResourcePatternResolver的具体实现

 * @author Juergen Hoeller
 * @author Colin Sampaleanu
 * @author Marius Bogoevici
 * @author Costin Leau
 * @since 1.0.2
 * @see #CLASSPATH_ALL_URL_PREFIX
 * @see org.springframework.util.AntPathMatcher
 * @see org.springframework.core.io.ResourceLoader#getResource(String)
 * @see java.lang.ClassLoader#getResources(String)
 */
public class PathMatchingResourcePatternResolver implements ResourcePatternResolver {
 private static final Log logger = LogFactory.getLog(PathMatchingResourcePatternResolver.class);
 private static Method equinoxResolveMethod;
 static {
  // Detect Equinox OSGi (e.g. on WebSphere 6.1)
  try {
   Class<?> fileLocatorClass = PathMatchingResourcePatternResolver.class.getClassLoader().loadClass(
     "org.eclipse.core.runtime.FileLocator");
   equinoxResolveMethod = fileLocatorClass.getMethod("resolve", URL.class);
   logger.debug("Found Equinox FileLocator for OSGi bundle URL resolution");
  }
  catch (Throwable ex) {
   equinoxResolveMethod = null;
  }
 }
 private final ResourceLoader resourceLoader;
 private PathMatcher pathMatcher = new AntPathMatcher();
 /**
  * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
  * <p>ClassLoader access will happen via the thread context class loader.
  * @see org.springframework.core.io.DefaultResourceLoader
  */
 public PathMatchingResourcePatternResolver() {
  this.resourceLoader = new DefaultResourceLoader();
 }
 /**
  * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
  * @param classLoader the ClassLoader to load classpath resources with,
  * or <code>null</code> for using the thread context class loader
  * at the time of actual resource access
  * @see org.springframework.core.io.DefaultResourceLoader
  */
 public PathMatchingResourcePatternResolver(ClassLoader classLoader) {
  this.resourceLoader = new DefaultResourceLoader(classLoader);
 }
 /**
  * Create a new PathMatchingResourcePatternResolver.
  * <p>ClassLoader access will happen via the thread context class loader.
  * @param resourceLoader the ResourceLoader to load root directories and
  * actual resources with
  */
 public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
  Assert.notNull(resourceLoader, "ResourceLoader must not be null");
  this.resourceLoader = resourceLoader;
 }
 /**
  * Return the ResourceLoader that this pattern resolver works with.
  */
 public ResourceLoader getResourceLoader() {
  return this.resourceLoader;
 }
 /**
  * Return the ClassLoader that this pattern resolver works with
  * (never <code>null</code>).
  */
 public ClassLoader getClassLoader() {
  return getResourceLoader().getClassLoader();
 }
 /**
  * Set the PathMatcher implementation to use for this
  * resource pattern resolver. Default is AntPathMatcher.
  * @see org.springframework.util.AntPathMatcher
  */
 public void setPathMatcher(PathMatcher pathMatcher) {
  Assert.notNull(pathMatcher, "PathMatcher must not be null");
  this.pathMatcher = pathMatcher;
 }
 /**
  * Return the PathMatcher that this resource pattern resolver uses.
  */
 public PathMatcher getPathMatcher() {
  return this.pathMatcher;
 }
 public Resource getResource(String location) {
  return getResourceLoader().getResource(location);
 }
 public Resource[] getResources(String locationPattern) throws IOException {
  Assert.notNull(locationPattern, "Location pattern must not be null");
  if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
   // a class path resource (multiple resources for same name possible)
   if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
    // a class path resource pattern
    return findPathMatchingResources(locationPattern);
   }
   else {
    // all class path resources with the given name
    return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
   }
  }
  else {
   // Only look for a pattern after a prefix here
   // (to not get fooled by a pattern symbol in a strange prefix).
   int prefixEnd = locationPattern.indexOf(":") + 1;
   if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
    // a file pattern
    return findPathMatchingResources(locationPattern);
   }
   else {
    // a single resource with the given name
    return new Resource[] {getResourceLoader().getResource(locationPattern)};
   }
  }
 }
 /**
  * Find all class location resources with the given location via the ClassLoader.
  * @param location the absolute path within the classpath
  * @return the result as Resource array
  * @throws IOException in case of I/O errors
  * @see java.lang.ClassLoader#getResources
  * @see #convertClassLoaderURL
  */
 protected Resource[] findAllClassPathResources(String location) throws IOException {
  String path = location;
  if (path.startsWith("/")) {
   path = path.substring(1);
  }
  Enumeration<URL> resourceUrls = getClassLoader().getResources(path);
  Set<Resource> result = new LinkedHashSet<Resource>(16);
  while (resourceUrls.hasMoreElements()) {
   URL url = resourceUrls.nextElement();
   result.add(convertClassLoaderURL(url));
  }
  return result.toArray(new Resource[result.size()]);
 }
 /**
  * Convert the given URL as returned from the ClassLoader into a Resource object.
  * <p>The default implementation simply creates a UrlResource instance.
  * @param url a URL as returned from the ClassLoader
  * @return the corresponding Resource object
  * @see java.lang.ClassLoader#getResources
  * @see org.springframework.core.io.Resource
  */
 protected Resource convertClassLoaderURL(URL url) {
  return new UrlResource(url);
 }
 /**
  * Find all resources that match the given location pattern via the
  * Ant-style PathMatcher. Supports resources in jar files and zip files
  * and in the file system.
  * @param locationPattern the location pattern to match
  * @return the result as Resource array
  * @throws IOException in case of I/O errors
  * @see #doFindPathMatchingJarResources
  * @see #doFindPathMatchingFileResources
  * @see org.springframework.util.PathMatcher
  */
 protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
  String rootDirPath = determineRootDir(locationPattern);
  String subPattern = locationPattern.substring(rootDirPath.length());
  Resource[] rootDirResources = getResources(rootDirPath);
  Set<Resource> result = new LinkedHashSet<Resource>(16);
  for (Resource rootDirResource : rootDirResources) {
   rootDirResource = resolveRootDirResource(rootDirResource);
   if (isJarResource(rootDirResource)) {
    result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern));
   }
   else if (rootDirResource.getURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
    result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher()));
   }
   else {
    result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
   }
  }
  if (logger.isDebugEnabled()) {
   logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);
  }
  return result.toArray(new Resource[result.size()]);
 }
 /**
  * Determine the root directory for the given location.
  * <p>Used for determining the starting point for file matching,
  * resolving the root directory location to a <code>java.io.File</code>
  * and passing it into <code>retrieveMatchingFiles</code>, with the
  * remainder of the location as pattern.
  * <p>Will return "/WEB-INF/" for the pattern "/WEB-INF/*.xml",
  * for example.
  * @param location the location to check
  * @return the part of the location that denotes the root directory
  * @see #retrieveMatchingFiles
  */
 protected String determineRootDir(String location) {
  int prefixEnd = location.indexOf(":") + 1;
  int rootDirEnd = location.length();
  while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) {
   rootDirEnd = location.lastIndexOf('/', rootDirEnd - 2) + 1;
  }
  if (rootDirEnd == 0) {
   rootDirEnd = prefixEnd;
  }
  return location.substring(0, rootDirEnd);
 }
 /**
  * Resolve the specified resource for path matching.
  * <p>The default implementation detects an Equinox OSGi "bundleresource:"
  * / "bundleentry:" URL and resolves it into a standard jar file URL that
  * can be traversed using Spring's standard jar file traversal algorithm.
  * @param original the resource to resolve
  * @return the resolved resource (may be identical to the passed-in resource)
  * @throws IOException in case of resolution failure
  */
 protected Resource resolveRootDirResource(Resource original) throws IOException {
  if (equinoxResolveMethod != null) {
   URL url = original.getURL();
   if (url.getProtocol().startsWith("bundle")) {
    return new UrlResource((URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, url));
   }
  }
  return original;
 }
 /**
  * Return whether the given resource handle indicates a jar resource
  * that the <code>doFindPathMatchingJarResources</code> method can handle.
  * <p>The default implementation checks against the URL protocols
  * "jar", "zip" and "wsjar" (the latter are used by BEA WebLogic Server
  * and IBM WebSphere, respectively, but can be treated like jar files).
  * @param resource the resource handle to check
  * (usually the root directory to start path matching from)
  * @see #doFindPathMatchingJarResources
  * @see org.springframework.util.ResourceUtils#isJarURL
  */
 protected boolean isJarResource(Resource resource) throws IOException {
  return ResourceUtils.isJarURL(resource.getURL());
 }
 /**
  * Find all resources in jar files that match the given location pattern
  * via the Ant-style PathMatcher.
  * @param rootDirResource the root directory as Resource
  * @param subPattern the sub pattern to match (below the root directory)
  * @return the Set of matching Resource instances
  * @throws IOException in case of I/O errors
  * @see java.net.JarURLConnection
  * @see org.springframework.util.PathMatcher
  */
 protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern)
   throws IOException {
  URLConnection con = rootDirResource.getURL().openConnection();
  JarFile jarFile;
  String jarFileUrl;
  String rootEntryPath;
  boolean newJarFile = false;
  if (con instanceof JarURLConnection) {
   // Should usually be the case for traditional JAR files.
   JarURLConnection jarCon = (JarURLConnection) con;
   ResourceUtils.useCachesIfNecessary(jarCon);
   jarFile = jarCon.getJarFile();
   jarFileUrl = jarCon.getJarFileURL().toExternalForm();
   JarEntry jarEntry = jarCon.getJarEntry();
   rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
  }
  else {
   // No JarURLConnection -> need to resort to URL file parsing.
   // We'll assume URLs of the format "jar:path!/entry", with the protocol
   // being arbitrary as long as following the entry format.
   // We'll also handle paths with and without leading "file:" prefix.
   String urlFile = rootDirResource.getURL().getFile();
   int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
   if (separatorIndex != -1) {
    jarFileUrl = urlFile.substring(0, separatorIndex);
    rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
    jarFile = getJarFile(jarFileUrl);
   }
   else {
    jarFile = new JarFile(urlFile);
    jarFileUrl = urlFile;
    rootEntryPath = "";
   }
   newJarFile = true;
  }
  try {
   if (logger.isDebugEnabled()) {
    logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
   }
   if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
    // Root entry path must end with slash to allow for proper matching.
    // The Sun JRE does not return a slash here, but BEA JRockit does.
    rootEntryPath = rootEntryPath + "/";
   }
   Set<Resource> result = new LinkedHashSet<Resource>(8);
   for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
    JarEntry entry = entries.nextElement();
    String entryPath = entry.getName();
    if (entryPath.startsWith(rootEntryPath)) {
     String relativePath = entryPath.substring(rootEntryPath.length());
     if (getPathMatcher().match(subPattern, relativePath)) {
      result.add(rootDirResource.createRelative(relativePath));
     }
    }
   }
   return result;
  }
  finally {
   // Close jar file, but only if freshly obtained -
   // not from JarURLConnection, which might cache the file reference.
   if (newJarFile) {
    jarFile.close();
   }
  }
 }
 /**
  * Resolve the given jar file URL into a JarFile object.
  */
 protected JarFile getJarFile(String jarFileUrl) throws IOException {
  if (jarFileUrl.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
   try {
    return new JarFile(ResourceUtils.toURI(jarFileUrl).getSchemeSpecificPart());
   }
   catch (URISyntaxException ex) {
    // Fallback for URLs that are not valid URIs (should hardly ever happen).
    return new JarFile(jarFileUrl.substring(ResourceUtils.FILE_URL_PREFIX.length()));
   }
  }
  else {
   return new JarFile(jarFileUrl);
  }
 }
 /**
  * Find all resources in the file system that match the given location pattern
  * via the Ant-style PathMatcher.
  * @param rootDirResource the root directory as Resource
  * @param subPattern the sub pattern to match (below the root directory)
  * @return the Set of matching Resource instances
  * @throws IOException in case of I/O errors
  * @see #retrieveMatchingFiles
  * @see org.springframework.util.PathMatcher
  */
 protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
   throws IOException {
  File rootDir;
  try {
   rootDir = rootDirResource.getFile().getAbsoluteFile();
  }
  catch (IOException ex) {
   if (logger.isWarnEnabled()) {
    logger.warn("Cannot search for matching files underneath " + rootDirResource +
      " because it does not correspond to a directory in the file system", ex);
   }
   return Collections.emptySet();
  }
  return doFindMatchingFileSystemResources(rootDir, subPattern);
 }
 /**
  * Find all resources in the file system that match the given location pattern
  * via the Ant-style PathMatcher.
  * @param rootDir the root directory in the file system
  * @param subPattern the sub pattern to match (below the root directory)
  * @return the Set of matching Resource instances
  * @throws IOException in case of I/O errors
  * @see #retrieveMatchingFiles
  * @see org.springframework.util.PathMatcher
  */
 protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String subPattern) throws IOException {
  if (logger.isDebugEnabled()) {
   logger.debug("Looking for matching resources in directory tree [" + rootDir.getPath() + "]");
  }
  Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern);
  Set<Resource> result = new LinkedHashSet<Resource>(matchingFiles.size());
  for (File file : matchingFiles) {
   result.add(new FileSystemResource(file));
  }
  return result;
 }
 /**
  * Retrieve files that match the given path pattern,
  * checking the given directory and its subdirectories.
  * @param rootDir the directory to start from
  * @param pattern the pattern to match against,
  * relative to the root directory
  * @return the Set of matching File instances
  * @throws IOException if directory contents could not be retrieved
  */
 protected Set<File> retrieveMatchingFiles(File rootDir, String pattern) throws IOException {
  if (!rootDir.exists()) {
   // Silently skip non-existing directories.
   if (logger.isDebugEnabled()) {
    logger.debug("Skipping [" + rootDir.getAbsolutePath() + "] because it does not exist");
   }
   return Collections.emptySet();
  }
  if (!rootDir.isDirectory()) {
   // Complain louder if it exists but is no directory.
   if (logger.isWarnEnabled()) {
    logger.warn("Skipping [" + rootDir.getAbsolutePath() + "] because it does not denote a directory");
   }
   return Collections.emptySet();
  }
  if (!rootDir.canRead()) {
   if (logger.isWarnEnabled()) {
    logger.warn("Cannot search for matching files underneath directory [" + rootDir.getAbsolutePath() +
      "] because the application is not allowed to read the directory");
   }
   return Collections.emptySet();
  }
  String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/");
  if (!pattern.startsWith("/")) {
   fullPattern += "/";
  }
  fullPattern = fullPattern + StringUtils.replace(pattern, File.separator, "/");
  Set<File> result = new LinkedHashSet<File>(8);
  doRetrieveMatchingFiles(fullPattern, rootDir, result);
  return result;
 }
 /**
  * Recursively retrieve files that match the given pattern,
  * adding them to the given result list.
  * @param fullPattern the pattern to match against,
  * with prepended root directory path
  * @param dir the current directory
  * @param result the Set of matching File instances to add to
  * @throws IOException if directory contents could not be retrieved
  */
 protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> result) throws IOException {
  if (logger.isDebugEnabled()) {
   logger.debug("Searching directory [" + dir.getAbsolutePath() +
     "] for files matching pattern [" + fullPattern + "]");
  }
  File[] dirContents = dir.listFiles();
  if (dirContents == null) {
   if (logger.isWarnEnabled()) {
    logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
   }
   return;
  }
  for (File content : dirContents) {
   String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
   if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
    if (!content.canRead()) {
     if (logger.isDebugEnabled()) {
      logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() +
        "] because the application is not allowed to read the directory");
     }
    }
    else {
     doRetrieveMatchingFiles(fullPattern, content, result);
    }
   }
   if (getPathMatcher().match(fullPattern, currPath)) {
    result.add(content);
   }
  }
 }
 /**
  * Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
  */
 private static class VfsResourceMatchingDelegate {
  public static Set<Resource> findMatchingResources(
    Resource rootResource, String locationPattern, PathMatcher pathMatcher) throws IOException {
   Object root = VfsPatternUtils.findRoot(rootResource.getURL());
   PatternVirtualFileVisitor visitor =
     new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher);
   VfsPatternUtils.visit(root, visitor);
   return visitor.getResources();
  }
 }
 /**
  * VFS visitor for path matching purposes.
  */
 private static class PatternVirtualFileVisitor implements InvocationHandler {
  private final String subPattern;
  private final PathMatcher pathMatcher;
  private final String rootPath;
  private final Set<Resource> resources = new LinkedHashSet<Resource>();
  public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) {
   this.subPattern = subPattern;
   this.pathMatcher = pathMatcher;
   this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
  }
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
   String methodName = method.getName();
   if (Object.class.equals(method.getDeclaringClass())) {
    if (methodName.equals("equals")) {
     // Only consider equal when proxies are identical.
     return (proxy == args[0]);
    }
    else if (methodName.equals("hashCode")) {
     return System.identityHashCode(proxy);
    }
   }
   else if ("getAttributes".equals(methodName)) {
    return getAttributes();
   }
   else if ("visit".equals(methodName)) {
    visit(args[0]);
    return null;
   }
   else if ("toString".equals(methodName)) {
    return toString();
   }
   
   throw new IllegalStateException("Unexpected method invocation: " + method);
  }
  public void visit(Object vfsResource) {
   if (this.pathMatcher.match(this.subPattern,
     VfsPatternUtils.getPath(vfsResource).substring(this.rootPath.length()))) {
    this.resources.add(new VfsResource(vfsResource));
   }
  }
  public Object getAttributes() {
   return VfsPatternUtils.getVisitorAttribute();
  }
  public Set<Resource> getResources() {
   return this.resources;
  }
  @SuppressWarnings("unused")
  public int size() {
   return this.resources.size();
  }
  public String toString() {
   StringBuilder sb = new StringBuilder();
   sb.append("sub-pattern: ").append(this.subPattern);
   sb.append(", resources: ").append(this.resources);
   return sb.toString();
  }
 }
}

四个属性,一个static block:

private static final Log logger = LogFactory.getLog(PathMatchingResourcePatternResolver.class);
 private static Method equinoxResolveMethod;
 static {
  // Detect Equinox OSGi (e.g. on WebSphere 6.1)
  try {
   Class<?> fileLocatorClass = PathMatchingResourcePatternResolver.class.getClassLoader().loadClass(
     "org.eclipse.core.runtime.FileLocator");
   equinoxResolveMethod = fileLocatorClass.getMethod("resolve", URL.class);
   logger.debug("Found Equinox FileLocator for OSGi bundle URL resolution");
  }
  catch (Throwable ex) {
   equinoxResolveMethod = null;
  }
 }
 private final ResourceLoader resourceLoader;
 private PathMatcher pathMatcher = new AntPathMatcher();

logger:对该类建立Log对象记录日志。

resourceLoader:该类持有的一个顶层接口的引用,该类实际的父子链关系:PathMatchingResourcePatternResolver-->ResourcePatternResolver-->ResourceLoader。持有该引用的目的应该是可以调用一下该类的其他具体实现方法。

pathMatcher:AntPathMatcher的一个instance,该类要用此类中关于路径匹配的一些函数。

静态快:主要探测eclipse开发环境中是否存在Equinox OSGi开发环境。(下一篇文章将对OSGI进行学习研究)

/**
  * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
  * <p>ClassLoader access will happen via the thread context class loader.
  * @see org.springframework.core.io.DefaultResourceLoader
  */
 public PathMatchingResourcePatternResolver() {
  this.resourceLoader = new DefaultResourceLoader();
 }
 /**
  * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
  * @param classLoader the ClassLoader to load classpath resources with,
  * or <code>null</code> for using the thread context class loader
  * at the time of actual resource access
  * @see org.springframework.core.io.DefaultResourceLoader
  */
 public PathMatchingResourcePatternResolver(ClassLoader classLoader) {
  this.resourceLoader = new DefaultResourceLoader(classLoader);
 } 
 /**
  * Create a new PathMatchingResourcePatternResolver.
  * <p>ClassLoader access will happen via the thread context class loader.
  * @param resourceLoader the ResourceLoader to load root directories and
  * actual resources with
  */
 public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
  Assert.notNull(resourceLoader, "ResourceLoader must not be null");
  this.resourceLoader = resourceLoader;
 }

三个构造方法:对本类持有的ResourceLoader接口实例进行实例化,用DefaultResourceLoader来实现,

不带参数的方法通过当前线程的类上下文加载器,带参数的构造通过特定的类加载器加载。第一个是指定特定的类加载器,第二个用哪个ResourceLoader进行加载。

  /**
  * Return the ResourceLoader that this pattern resolver works with.
  */
 public ResourceLoader getResourceLoader() {
  return this.resourceLoader;
 }
 /**
  * Return the ClassLoader that this pattern resolver works with
  * (never <code>null</code>).
  */
 public ClassLoader getClassLoader() {
  return getResourceLoader().getClassLoader();
 }

第一个方法获得与当前pattern solver协作的ResourceLoader,第二个获得ClassLoder和第一个不同,通过构造方法进行区分。

/**
  * Set the PathMatcher implementation to use for this
  * resource pattern resolver. Default is AntPathMatcher.
  * @see org.springframework.util.AntPathMatcher
  */
 public void setPathMatcher(PathMatcher pathMatcher) {
  Assert.notNull(pathMatcher, "PathMatcher must not be null");
  this.pathMatcher = pathMatcher;
 }
 /**
  * Return the PathMatcher that this resource pattern resolver uses.
  */
 public PathMatcher getPathMatcher() {
  return this.pathMatcher;
 }

插入了一个PathMatcher,得到了究竟是哪个PathMatcher呢?

 public Resource getResource(String location) {
  return getResourceLoader().getResource(location);
 }

通过路径获得资源。

 public Resource[] getResources(String locationPattern) throws IOException {
  Assert.notNull(locationPattern, "Location pattern must not be null");
  if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
   // a class path resource (multiple resources for same name possible)
   if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
    // a class path resource pattern
    return findPathMatchingResources(locationPattern);
   }
   else {
    // all class path resources with the given name
    return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
   }
  }
  else {
   // Only look for a pattern after a prefix here
   // (to not get fooled by a pattern symbol in a strange prefix).
   int prefixEnd = locationPattern.indexOf(":") + 1;
   if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
    // a file pattern
    return findPathMatchingResources(locationPattern);
   }
   else {
    // a single resource with the given name
    return new Resource[] {getResourceLoader().getResource(locationPattern)};
   }
  }
 }

三种情况,假前缀,相对路径,绝对路径。

/**
  * Find all class location resources with the given location via the ClassLoader.
  * @param location the absolute path within the classpath
  * @return the result as Resource array
  * @throws IOException in case of I/O errors
  * @see java.lang.ClassLoader#getResources
  * @see #convertClassLoaderURL
  */
 protected Resource[] findAllClassPathResources(String location) throws IOException {
  String path = location;
  if (path.startsWith("/")) {
   path = path.substring(1);
  }
  Enumeration<URL> resourceUrls = getClassLoader().getResources(path);
  Set<Resource> result = new LinkedHashSet<Resource>(16);
  while (resourceUrls.hasMoreElements()) {
   URL url = resourceUrls.nextElement();
   result.add(convertClassLoaderURL(url));
  }
  return result.toArray(new Resource[result.size()]);
 }

findAllClassPathResources,获得所有的Resource过滤装进数组,Set-toArray。

 /**
  * Convert the given URL as returned from the ClassLoader into a Resource object.
  * <p>The default implementation simply creates a UrlResource instance.
  * @param url a URL as returned from the ClassLoader
  * @return the corresponding Resource object
  * @see java.lang.ClassLoader#getResources
  * @see org.springframework.core.io.Resource
  */
 protected Resource convertClassLoaderURL(URL url) {
  return new UrlResource(url);
 }

默认实现被URL包装成了UrlResource。

 /**
  * Find all resources that match the given location pattern via the
  * Ant-style PathMatcher. Supports resources in jar files and zip files
  * and in the file system.
  * @param locationPattern the location pattern to match
  * @return the result as Resource array
  * @throws IOException in case of I/O errors
  * @see #doFindPathMatchingJarResources
  * @see #doFindPathMatchingFileResources
  * @see org.springframework.util.PathMatcher
  */
 protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
  String rootDirPath = determineRootDir(locationPattern);
  String subPattern = locationPattern.substring(rootDirPath.length());
  Resource[] rootDirResources = getResources(rootDirPath);
  Set<Resource> result = new LinkedHashSet<Resource>(16);
  for (Resource rootDirResource : rootDirResources) {
   rootDirResource = resolveRootDirResource(rootDirResource);
   if (isJarResource(rootDirResource)) {
    result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern));
   }
   else if (rootDirResource.getURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
    result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher()));
   }
   else {
    result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
   }
  }
  if (logger.isDebugEnabled()) {
   logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);
  }
  return result.toArray(new Resource[result.size()]);
 }

借助PathMatcher来获取所有资源,包括jar,zip和文件系统的资源。

哎呀,这样好麻烦啊!

resourceLoader的设计通过getResource得到resource资源,加载到classLoader中,然后供applicationContext调用,通过bean模块具体实例化bean,如果是这样IOC的内部就是这样了,下一篇应该是对bean的学习解析。

转载于:https://my.oschina.net/stuspring/blog/471642

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值