Spring Bean Load External Resources Example

Spring framework provide methods to load external resource data (text file, image file, binary file) into your application. The external resource can be a local file, a url locator file and the file saved in the ClassPath. This article will show you examples about how to do that.

1. Load external resource by ApplicationContext.

Below example code demonstrate how to use Spring ApplicationContext to load external resource. First you should get the spring ApplicationContext object.

ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("BeanSettings.xml");

Load from local file system

  1. Save file at C:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt.
  2. Load it in java code.
    Resource resource = springAppCtx.getResource("file:C:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt");

Load from classpath

  1. Save the file under classpath such as com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt or java project resources file folder.
    save external resource in classpath folder
  2. Load it in java code.
    	Resource res = springAppCtx.getResource("classpath:com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt");
    	
            Resource res = springAppCtx.getResource("classpath:SpringExternalResourceData.txt");
    

Load from url

  1. Upload the file to a web server. And access it with below url.
    http://www.dev2qa.com/demo/spring/load_resource/example_data.txt.
  2. Load it in java code.
    Resource res = springAppCtx.getResource("url:http://www.dev2qa.com/demo/spring/load_resource/example_data.txt");
    

ApplicationContextLoadResource.java

public class ApplicationContextLoadResource {

	public static void main(String[] args) {
		
		try
		{
		   	// Initiate Spring application context, this line code will invoke bean initialize method.
			ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("BeanSettings.xml");
	
			// Get from local file.
			Resource res = springAppCtx.getResource("file:C:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt");
			
			// Get from a url file.
			//Resource res = springAppCtx.getResource("url:http://www.dev2qa.com/demo/spring/load_resource/example_data.txt");
			
			// Get from a file in classpath.
			//Resource res = springAppCtx.getResource("classpath:com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt");
			//Resource res = springAppCtx.getResource("classpath:SpringExternalResourceData.txt");
			
			InputStream inputStream = res.getInputStream();
			
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			
			BufferedReader bufferReader = new BufferedReader(inputStreamReader);
			
			String line = bufferReader.readLine();
			
			while(line!=null)
			{
				System.out.println(line);
				
				line = bufferReader.readLine();
			}
		
		}catch(IOException ex)
		{
			ex.printStackTrace();
		}
		
	}

}

2. Load external resource in spring bean.

  1. org.springframework.core.io.ResourceLoader can be used in spring bean to load external resources.
  2. To use it in your spring bean class, you should make your class implements org.springframework.context.ResourceLoaderAware interface.
  3. Override it’s setResourceLoader(ResourceLoader resourceLoader)  method, spring framework will use this method to inject a ResourceLoader object into your bean class automatically.
  4. Assign the spring framework injected ResourceLoader object to a bean local property.
  5. Use bean local ResourceLoader object to load external resources.
public class SpringBeanLoadResource implements ResourceLoaderAware {
	// bean property.
	private ResourceLoader resourceLoader;

	// This method will be used by Spring framework to inject resouceLoader instance into this bean class.
	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		// Assign the framework injected resourceLoader to a local variable for later use.
		this.resourceLoader = resourceLoader;
	}
	
	// Use resourceloader to get Resource object by locator.
	public Resource getExternalResource(String resourceLocator)
	{
		return this.resourceLoader.getResource(resourceLocator);
	}

	public static void main(String[] args) {
		
		try
		{
		   	// Initiate Spring application context, this line code will invoke bean initialize method.
			ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("BeanSettings.xml");
			
			
			SpringBeanLoadResource springBeanLoadResource = (SpringBeanLoadResource)springAppCtx.getBean("springBeanLoadResource");
	
			// Get from local file.
			//Resource res = springBeanLoadResource.getExternalResource("file:C:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt");
			
			// Get from a url file.
			//Resource res = springBeanLoadResource.getExternalResource("url:http://www.dev2qa.com/demo/spring/load_resource/example_data.txt");
			
			// Get from a file in classpath.
			//Resource res = springBeanLoadResource.getExternalResource("classpath:com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt");
			Resource res = springBeanLoadResource.getExternalResource("classpath:SpringExternalResourceData.txt");
			
			InputStream inputStream = res.getInputStream();
			
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			
			BufferedReader bufferReader = new BufferedReader(inputStreamReader);
			
			String line = bufferReader.readLine();
			
			while(line!=null)
			{
				System.out.println(line);
				
				line = bufferReader.readLine();
			}
		
		}catch(IOException ex)
		{
			ex.printStackTrace();
		}
		
	}
}

Download “Spring-Bean-Load-External-Resources-Example.zip” Spring-Bean-Load-External-Resources-Example.zip – Downloaded 153 times – 2 KB

READ :   Autowire Spring Bean Constructor Injection Examples

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.