Read file in Spring

Spring ResourceLoader provides a unified getResource() method for us to retrieve an external resource by a resource path.java

1. Resource interface represents a resource

Resource is a general interface in Spring for representing an external resource.spring

 

Spring provides following 6 implementations for the Resource interface.app

  1. UrlResource
  2. ClassPathResource
  3. FileSystemResource
  4. ServletContextResource
  5. InputStreamResource
  6. ByteArrayResource

We can specify different prefixes for creating path to load resources from different locations.ide

Prefix Example Explanation
classpath: classpath:com/myapp/config.xml Loaded from the classpath.
file: file:///data/config.xml Loaded as a URL from the filesystem.
http: https://myserver/logo.png Loaded as a URL.
(none) /data/config.xml Depends on the underlying ApplicationContext.

 

2. ResourceLoader

It is used for loading resources. It has two methods:post

//Return a Resource handle for the specified resource location.
Resource getResource(String location)

The getResource() method will decide which Resource implementation to instantiate according to the resource path.this

Resource banner = resourceLoader.getResource("claspath:classpathdata.txt");

 

3. Loading resource with ApplicationContext

In Spring, all application contexts implement the ResourceLoader interface. Therefore, all application contexts may be used to obtain Resource instances.spa

 

To get the reference of ApplicationContext, implement the ApplicationContextAware interface.code

Resource banner = ctx.getResource("claspath:classpathdata.txt");

 

4. Loading resource with ResourceLoaderAware

To demonstrate the various examples below, I have placed a file with matching name in different locations and I will show to load each one of them.server

ResourceLoaderWithResourceLoaderAware.java is written as below which print the content of loaded resource file into console.xml

package com.example.readfile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

public class ResourceLoaderWithResourceLoaderAware implements ResourceLoaderAware
{
private ResourceLoader resourceLoader;

public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

public void showResourceData() throws IOException
{
//This line will be changed for all versions of other examples
Resource banner = resourceLoader.getResource("classpath:classpathdata.txt");

InputStream in = banner.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
reader.close();
}
}

And applicationContext.xml file entry for this file is as below:

<bean id="resourceLoaderWithResourceLoaderAware" class="com.example.readfile.ResourceLoaderWithResourceLoaderAware"></bean>

To test the ResourceLoaderWithResourceLoaderAware bean and call the showResourceData() method, below code has been used:

package com.example.readfile;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) throws Exception {        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        ResourceLoaderWithResourceLoaderAware customResourceLoader =            (ResourceLoaderWithResourceLoaderAware) context.getBean("resourceLoaderWithResourceLoaderAware");        customResourceLoader.showResourceData();    }}
相關文章
相關標籤/搜索