Resources API in Spring – Part 2 (ResourceLoaderAware)

In the previous post I discussed about the external resource loading in Spring using Resource Loader interface, and setter methods. In this post I shall discuss about another form of resource loading through ResourceLoaderAware interface. Prior to release of Spring 2.5, this technique was used to load the external resources. Post Spring 2.5, you can rely upon autowiring of the ResourceLoader as an alternative to implementing the ResourceLoaderAware interface.

The ResourceLoaderAware interface is a special marker interface, identifying objects that expect to be provided with a ResourceLoader reference.

also read: follow us on @twitter and @facebook

Example for Using ResourceLoaderAware

The example below demonstrates the use of ResourceLoaderAware to load the external resources. Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

  1. Create a project: Create a project with a name SpringExternalResourceExample and create a package com.javabeat under the src directory in the created project.
  2. Add Libraries: Add required Spring libraries using Add External JARs option as explained in the article Customizing callback methods.
  3. Create configuration file: Create XML based configuration file Beans.xml under src directory.
  4. Create source files: Create Java classes MainApp and ProductService under the com.javabeat package.

Contents of are:

package com.javabeat;

import org.springframework.context.ResourceLoaderAware;

public class ProductService implements ResourceLoaderAware {

	private ResourceLoader resourceLoader;

	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;

	public Resource getResource(String location) {
		return resourceLoader.getResource(location);

Contents of Beans.xml are:

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""

      <bean id="productService" class="com.javabeat.ProductService" >


Contents of are:

package com.javabeat;


import org.springframework.context.ApplicationContext;

public class MainApp {
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
		ProductService productService = (ProductService) ctx

		try {
			Resource resource = productService
			InputStream is = resource.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));

			String line;
			while ((line = br.readLine()) != null) {

		} catch (IOException e) {



We get the resource from the bean productService.

Now execute the code and the following output appears on the console:


Constructing application contexts

ApplicationContext is an interface only. You have to instantiate an implementation of it. An application context constructor (for a specific application context type) generally takes a string or array of strings as the location path(s) of the resource(s) such as XML files that make up the definition of the context. For example:

ApplicationContext ctx = new ClassPathXmlApplicationContext("com/javabeat/beans.xml");

The ClassPathXmlApplicationContext implementation builds an application context by loading an XML
configuration file from the classpath, when the location path doesn’t have a prefix. Besides ClassPathXmlApplicationContext, several other ApplicationContext implementations are provided by Spring. FileSystemXmlApplicationContext is used to load XML configuration files from the file system or from URLs, while XmlWebApplicationContext and XmlPortletApplicationContext can be used in web and portal applications only.

The classpath*: prefix

When constructing an XML-based application context, a location string may use the special classpath “*:” prefix: An example:

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");

The classpath*:conf/appContext.xml simply means that all appContext.xml files under conf folders in all your jars on the classpath will be picked up and joined into one big application context.


In this post we saw how to load resources using ResourceLoaderAware interface. We also saw how to construct the application contexts and use wild card in application context constructor using the classpath*: prefix.Please refer here for the official documentation on resource API. If you are interested in receiving the future articles, please subscribe here. follow us on @twitter and @facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!