Resources and ResourceLoaders in Spring

Introduction

This is a series and it would make a lot of sense if read in a sequence. You can find all the posts under the heading “Spring Beginners to Professionals“. An enterprise level application is found to deal with a lot of resources, both static and dynamic. For e.g. properties files, images, xmls etc. In this post we will discuss about resources and ResourceLoaders in Spring.

Resources and ResourceLoaders in Spring

It is important to understand, how Spring treats a Resource and what does it mean when we say Resource in a Spring application.

What is a Resource?

A resource in this context by definition is a source of information which is primarily represented by a file existing on a filesystem, a stream over a network which can be accessed using a URL or any similar component.

Spring provides a richer abstraction over resources and these resources are loaded into the application context using ResourceLoaders. Spring categorizes resources in several types like

  • ClassPath resource,
  • URL resource,
  • ByteArray resource,
  • FileSystem resource,
  • InputStream resource and few more..

All these resource types are represented by their specialized classes.

For e.g.:

  • A resource loader can load an XML file residing on a network and represent it as an instance of URLResource
  • Similarly, another another resource loader can load a properties file residing in the class path and represent it as an instance of ClassPathResource.

What is a ResourceLoader ?

The Resource Loader understands the mechanism of loading a resource based on the supplied resource identifier or path. It uses a PathMatchingResourcePatternResolver to do so. It can successfully resolve URL patterns like (classpath:, file:, ftp:, jar: zip: etc.) in the resource path.

Spring Team suggests that the ResourceLoaders can be treated as utility classes. There should be no harm in using them wherever required even if it couples the application code to spring APIs. This is because the functionality of ResourceLoader is pretty simple and common. I also agree with them.

Relation between ApplicationContext and ResourceLoader

As we know that ApplicationContext may require external resources. Hence, every ApplicationContext implementation is a ResourceLoader by default which lets you use the underlying application context as a resource loader.

In such a case, if you try to load a resource using the ApplicationContext and do not qualify the resource path with protocol prefix (http:, classpath: etc), it would automatically use the protocol based on the type of the ApplicationContext.

Foe e.g.: The ClassPathXMLApplicationContext will load the resource from classpath for any path of form path/lib/resource.xml whereas the FileSystemXMLApplicationContext will load the same resource from an absolute location of the file system.

Here is a snippet which shows the usage of ApplicationContext as a resource loader.

This will load the resourcepath.xml if it exists in the classpath.

Summary

This post describes the Resources and ResourceLoaders in Spring. Few points worth noting are:

  • An ApplicationContext is also a ResouceLoader.
  • The ApplicationContext automcatically assumes the protocol based on its type, if there is no protocol in the resource path

Resources are inherent part of the application and Spring provides a really elegant way of loading that. Spring also provides protocols like classpath: which is helpful in reading resources from classpath of the application.

Stay Connected