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 ProductService.java are:

package com.javabeat;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

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="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx-2.5.xsd


http://www.springframework.org/schema/aop


http://www.springframework.org/schema/aop/spring-aop-2.5.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-2.5.xsd">

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

    </beans>

Contents of MainApp.java are:

package com.javabeat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

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

		try {
			Resource resource = productService
					.getResource("file:c:\\SampleResourceFile.txt");
			InputStream is = resource.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));

			String line;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			br.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

We get the resource from the bean productService.

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

**********************************
HI I'M AN EXTERNAL RESOURCE FILE
**********************************

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.

Summary

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.

Comments

comments

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Speak Your Mind

*