Spring Resource loader with getResource() example

Spring’s resource loader provides a very generic getResource() method to get the resources like (text file, media file, image file…) from file system , classpath or URL. You can get the getResource() method from the application context.

Here’s an example to show how to use getResource() to load a text file from

1. File system


Resource resource = appContext.getResource("file:c:\\testing.txt");

2. URL path


Resource resource = 
          appContext.getResource("url:http://www.yourdomain.com/testing.txt");

3. Class path


Resource resource = 
          appContext.getResource("classpath:com/mkyong/common/testing.txt");

You just need to specify the resource location, and the Spring will handle the rest and return you a Resource object.

Full example with getResource() method.


package com.mkyong.common;

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 App 
{
    public static void main( String[] args )
    {
    	ApplicationContext appContext = 
    	   new ClassPathXmlApplicationContext(new String[] {"If-you-have-any.xml"});

    	Resource resource = 
           appContext.getResource("classpath:com/mkyong/common/testing.txt");
    	
    try{
     	  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();
    	}
    	
    }
}

Bean resource loader (ResourceLoaderAware)

Since bean does not have the application context access, how can a bean access a resources? The workaround is implement the ResourceLoaderAware interface and create setter method for ResourceLoader object. Spring will DI the resource loader into your bean.


package com.mkyong.customer.services;

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

public class CustomerService implements ResourceLoaderAware
{
	private ResourceLoader resourceLoader;
	
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}
		
	public Resource getResource(String location){
		return resourceLoader.getResource(location);
	}
}

Bean configuration file


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

   <bean id="customerService" 
           class="com.mkyong.customer.services.CustomerService" />
   		
</beans>

Run it


package com.mkyong.common;

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;

import com.mkyong.customer.services.CustomerService;
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext appContext = 
    	   new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
	
    	CustomerService cust = 
           (CustomerService)appContext.getBean("customerService");
    	
    	Resource resource = 
            cust.getResource("classpath:com/mkyong/common/testing.txt");
    	
    try{
          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();
    	}
    	
    }
}

Now you can get the resources from a bean.

Conclusion

Without this getResource() method, you will need to deal with different resources with different solution, like File object for file system resource, URL object for URL resource. Spring really did a good job with this super generic getResource() method, it really save our time to deal with resources.

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
felixna
Guest
felixna

Why the workaround with “implements ResourceLoaderAware”? You can instanciate the Springs DefaultResourceLoader and use it directly:

ResourceLoader rl = new DefaultResourceLoader();
Resource r1 = rl.getResource(“classpath:someClasspathResource”);
Resource r2 = rl.getResource(“file:/some/file/resource”);

No need for the code to run in an ApplicationContext as Spring Bean!

Hemant
Guest
Hemant

can you share also how to write to file in class path.
currently you are reading file.what about writing to text file from spring controller.

Hari
Guest
Hari

Hi Mykong, thanks for the example. I have a key file in webapps/resources directory which I have to pass as a char[] for another method to execute ssh connection. how do I access the file under webapps dir? and how do I convert the resource object to char[]?

Deepak Varier
Guest
Deepak Varier

Thanks for the tutorial. We can get the application context in a bean by implementing ApplicationContextAware interface. But for problem above, ResourceLoadrerAware is the straight solution. :)

Arun Chauhan
Guest
Arun Chauhan

Thanks for nice article. My problem is that i want to do it by simply by using Wicket 1.5. I have gone through many example but I am not getting the proper way. I want to load the image file from file system in my wicket application.

Any help and advice appreciated! Thanks in advance!

DVenere
Guest
DVenere

You save me over and over again, thank you.