Spring Bean Load External Resources Example

Spring framework provides 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, or a file that is saved in the ClassPath. This article will show you examples of how to do that.

1. Load External Resource By ApplicationContext.

  1. The below example code demonstrates how to use Spring ApplicationContext to load external resources.
  2. First, you should get the spring ApplicationContext object.
    ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("BeanSettings.xml");

1.1 Load from the local file system steps.

  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");

1.2 Load from classpath steps.

  1. Save the file under classpath such as com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt or java project resources file folder (src/main/resources).
  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");
    

1.3 Load from URL steps.

  1. Upload the file to a web server. And access it with the 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");
    

1.4 ApplicationContextLoadResource.java

  1. 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 resources 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();
    		}
    		
    	}
    }
    

Leave a Comment

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.