Saturday, 11 June 2011

Loading Resources Using Spring’s ApplicationContext

Using the ApplicationContext, the guys at Spring provide a uniform way for your application to access various types of resources. These may be a file on the file system or on the classpath or something more remote on the Internet.

The main interface to be aware of is Resource which defines several self-explanatory methods. When using this mechanism all you need to do is supply a URI. The usual formats of file:, http: are supported together with the familiar Spring specific classpath: protocol.

The following Spring classes implement the Resource interface and these are:
  • UrlResource
  • ClassPathResource
  • FileSystemResource
  • ServletContextResource
  • InputstreamResource
  • ByteArrayResource
…but, you don’t really need to know about these as ApplicationContext also implements the Resource interface and will do all the work for you.

The code below demonstrates how to use this mechanism to read a file on the file system and classpath and to read data from a URL.

public class Example3_Application_Resource {

public static void main(String[] args) {

try {
       * Get hold of an application context, it doesn't matter which file in this case...
ApplicationContext ctx = new FileSystemXmlApplicationContext(
Constants.PATH_TO_RESOURCES + "example3_ApplicationContext_Events.xml");

} catch (IOException e) {
System.out.println("IO error: " + e.getMessage());

private static void demoFileResource(ApplicationContext ctx) throws IOException {

String fileName = "file:///tmp/example3_ApplicationContext_Events.xml";
    Resource fileResource = loadResource
(ctx, fileName);

    File file = fileResource.getFile
("File is: " + file.getAbsolutePath());

(fileResource, "This is the file: ");

private static Resource loadResource(ApplicationContext ctx, String resourceName) {

Resource resource = ctx.getResource(resourceName);
("The resource name is: " + resource.getFilename());
return resource;

private static void displayURIInfo(Resource resource) throws IOException {

URI url = resource.getURI();
("Host: " + url.getHost());
("Port: " + url.getPort());
("Path: " + url.getPath());

private static void readResource(Resource resource, String message) throws IOException {

if (testResource(resource)) {
InputStream inp = resource.getInputStream();
(inp, message);

private static void printResource(InputStream inp, String msg) throws IOException {

byte[] buffer = new byte[8192];
int len;
while ((len = != -1) {
System.out.print(new String(buffer, 0, len));

public static boolean testResource(Resource resource) {

boolean retVal = true;
if (!resource.exists()) {
System.out.println("resource does not exist: " + resource.getFilename());
      retVal =
return retVal;

private static void demoClasspathResource(ApplicationContext ctx) throws IOException {

Resource classpathResource = loadResource(ctx,

(classpathResource, "This is the classpath file: ");

private static void demoURLResource(ApplicationContext ctx) throws IOException {

Resource urlResource = loadResource(ctx, "");

     * This doesn't work if the source is a URL File file = urlResource.getFile();
     * file.getAbsolutePath());
readResource(urlResource, "This is the URL: ");

This example requires the following JAR files on the classpath:
  • spring-context.jar - for the ApplicationContext stuff.
  • spring-core.jar - for the FileSystemResource class

No comments: