Spring's Resource

Source: Internet
Author: User

Spring abstracts the resource interface because traditional Java uses URLs and standard handler to dispose of resources. However, one limitation is that the resources under CLASSPATH cannot be read directly. So spring defines a set of interfaces. This set of interfaces is divided into two parts, resource and Resourceloader, which are abstractions of resources, which are responsible for loading resources. Spring's ApplicationContext implements the Resourceloader interface.

Resource

First look at the interface definition of resource.

Public interface Resource extends Inputstreamsource {Boolean exists ();    Boolean isreadable (); /** * Return Whether this resource represents a handle with an open * stream.    If true, the InputStream cannot be read multiple times, * and must is read and closed to avoid resource leaks.    * <p>will is {@code false} for typical resource descriptors.    */Boolean IsOpen ();    /** * Return A URL handle for this resource.    * @throws IOException If the resource cannot be resolved as URL, * i.e. if the resource are not available as descriptor    */URL GetURL () throws IOException;    /** * Return A URI handle for this resource.    * @throws IOException If the resource cannot be resolved as URI, * i.e. if the resource are not available as descriptor      */URI GetURI () throws IOException;      File GetFile () throws IOException;       Long ContentLength () throws IOException;    */Long LastModified () throws IOException; /** * Create A resourceRelative to this resource. * @param relativepath the relative path (relative to this resource) * @return The resource handle for the relative Reso Urce * @throws IOException If the relative resource cannot be determined */resource createrelative (String relativ      Epath) throws IOException;      String GetFileName (); String getdescription (); }public interface Inputstreamsource {InputStream getinputstream () throws IOException;}

The following interfaces are straightforward and the only thing to note is the method IsOpen (), which returns true to indicate that the resource can be read multiple times and needs to be closed by the consumer. The typical implementation returns false.

The resource interface has sub-interfaces and several implementation classes, and its basic structure inherits from the following:



The first is the inclusion of two sub-interfaces Writableresource extends the writable nature of the resource, contextresource the extension, and reads the resource in a context.

All of the specific implementation classes inherit Abstractresource. As can be seen from the name, different implementation classes are responsible for different forms of resources.

where filesystemresource extendsabstractresource implements Writableresource, this is a very common way to expand.

Resourceloader

Spring provides a number of different types of resource,resourceloader that are responsible for creating different resource instances based on resource descriptors (usually strings (arrays) and wildcard characters), depending on the situation.


The interface is defined as follows:

Public interface Resourceloader {    /** Pseudo URL prefix for loading from the class path: "Classpath:" */   String CL Asspath_url_prefix = Resourceutils.classpath_url_prefix;    Resource getresource (String location);       ClassLoader getClassLoader (); }


Defines the method that loads the resource and exposes the Resourceloader class loader, which implements the class and sub-interface integration structure as follows:



The Applicationcontex we use are inherited by Resourceloader, where Defaultresourceloader implementations getresource as follows, loading resources in three scenarios:

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);}}   }


Here's how to use Resourceloader when spring builds ApplicationContext, where you can just pick up some of the code that loads the XML configuration. The following methods are Abstractbeandefinitionreader:

public int loadbeandefinitions (String location, set<resource> actualresources) throws beandefinitionstoreexception {      Resourceloader resourceloader = Getresourceloader ();      if (Resourceloader = = null) {         throw new Beandefinitionstoreexception (                "Cannot import bean definitions from Locati On ["+ Location +"]: no Resourceloader available ");      }       if (Resourceloader instanceof resourcepatternresolver) {         //support wildcard         try {            resource[] resources = (( Resourcepatternresolver) resourceloader). getresources (location);      else {         //Can only load the single resources by absolute URL.         Resource Resource = resourceloader.getresource (location);         }         return loadcount;      }   } @Override
The return is ApplicationContext public   resourceloader Getresourceloader () {      return this.resourceloader;   }

End

This article simply analyzes spring's resource framework and picks out the code that spring uses resource when loading the bean definition, to understand this and to lay a small foundation for understanding the Spring container's startup process. This article is written according to spring source code, because this part is quite simple, so many parts (such as the basic implementation of Abstractresource) are not listed.

Spring's Resource

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.