ADS

Accessing Resources From Spring Application Context

The following example illustrates accessing resources (both external to the project and internal) from the spring application context.

To learn this, we need to first learn about the org.springframework.core.io.Resource interface. This interface contains some methods which you can learn from the above url. All those methods are are self-explanatory. One method that I would like to pin point is the createRelative() method.
This method takes in a string, the relative path of the current resource and creates a resource object for it. An example, will give you a better understand. For example, consider a resource 'r' pointing to the E:/java/sample.txt. Now, when i call r.createRelative("ding/asdf.txt"); then I will get a Resource object pointing to E:/java/ding/asdf.txt

Create a project in eclipse

So, I'll be naming the project as spring1 since this is the first demo. Note that I haven't used STS (Spring Tool Suite) which most will probably be using. Rather, I have just downloaded the required jar files we need to create our project and included them in the library. Therefore, we will be carrying on creating a simple eclipse project and adding the necessary spring jars. I am using Eclipse KEPLER.

  1. File -> New -> Project -> Java Project
  2. Give the project name spring28 and click Finish 
  3. Now, the project is created.
  4. Under the Project Explorer (in the sidebar) you will see spring1. If you aren't able to see Project Explorer, go to Window sajian -> Show view -> Project Explorer.
  5. Right click on the spring28 and click Properties
  6. On the left side, click on Java Build Path.
  7. Select the Libraries tab and then click on Add External Jars
  8. Now, add these jar files starting with: spring-beans, spring-context, spring-core, commons-logging
  9. Click on OK and then you are ready.

Project structure


SpringPrg.java - Main class

package spring28;

import java.io.InputStream;

import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.Resource;

public class SpringPrg {

    @SuppressWarnings("resource")
    public static void main(String args[])
    {
        GenericXmlApplicationContext gc=new GenericXmlApplicationContext();
       
        Resource r1=gc.getResource("classpath:resources/sample.txt");
        Resource r2=gc.getResource("file:///e:/java/ascii.java");
       
        displayInfo(r1);
        displayInfo(r2);
    }
   
    public static void displayInfo(Resource r)
    {
        System.out.println("The file name is "+r.getFilename());
        System.out.println("The description is "+r.getDescription());
       
        try
        {
        System.out.println("The contents of the file is \n"+getContentAsString(r.getInputStream()));
        }catch(Exception e){}
        System.out.println("-------------------------------------");
    }
   
    public static String getContentAsString(InputStream is) throws Exception
    {
    StringBuffer sb=new StringBuffer();
    int k;
   
        while((k=is.read())!=-1)
        {
            sb.append((char)k);
        }
       
    return sb.toString();
    }
   
}

Subscribe to receive free email updates:

ADS