"Spring Revelation" (10)----ApplicationContext Unified Resource Loading strategy

Source: Internet
Author: User

Tag: www source string application oca XML bulk location file

Spring's Resource

The resource interface is used internally by the spring framework as an abstraction and interface for all resources. For example:

Beanfactory beanfactory = new Xmlbeanfactory (New Classpathresource ("..."));

Classpathresource is a specific type of implementation of resource that represents a resource that is actually located in Classpath.

Resource interface can be based on the different types of resources, or resources in different situations, give the corresponding specific implementation:

A. Bytearrayresource the data provided by an array of byte (byte) as a resource

B. Classpathresource the implementation loads specific resources and encapsulates them from the Java application's Classpath

C. Filesystemresource to the Java.io.File type of encapsulation, so we can be a file or URL in the form of the type of resources to access, as long as can be dealt with file, basically with Filesystemresource can also.

D. Urlresource the implementation class for locating a specific resource through Java.net.URL

If the above resources do not meet the requirements, can be based on the corresponding scenario to give their own implementation, just implement the resource interface is.


Resourceloader's job is to find and locate resources. The specific resource lookup location strategy is given by the corresponding Resourceloader implementation class. Resourceloder is defined as follows:

Public interface Resourceloader {    String classpath_url_prefix = resourceutils.classpath_url_prefix;    Resource getresource (String location);    ClassLoader getClassLoader ();} 

  Resourceloader----Resourcepatternresolver for bulk lookups

Resourcepatternresolver can return multiple resource instances each time according to the specified resource path matching pattern

Defined as follows:

Public interface Resourcepatternresolver extends resourceloader {    String classpath_all_url_prefix = "CLASSPATH *:";    Resource[] Getresources (String locationpattern) throws IOException;}  

Hierarchy of Resourcehe and Resourceloader (excerpt from http://www.jianshu.com/p/9cdd6d750216):


  ApplicationContext and Resourceloader

Look again at the beanfactory and ApplicationContext inheritance diagram:

ApplicationContext inherited the Resourcepatternresolver, of course, the indirect implementation of the Resourceloader interface. So, the ApplicationContext implementation can be seen as a resourceloader or even a resourcepatternresolver. This is the truth of ApplicationContext support for the unified resource loading strategy within spring.

In general, all ApplicationContext implementation classes inherit abstractapplicationcontext directly or indirectly, from which you can see all the relationships between ApplicationContext and Resourceloader

So abstractapplication can be used as Resourceloader and Resourcepatternresolver.

"Spring Revelation" (10)----ApplicationContext Unified Resource Loading strategy

Related Article

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.