Spring Read Resource Example

Learn different ways to load resources or files (e.g. text files, XML files, properties files, or image files) into the Spring application context. Spring ResourceLoader provides a unified getResource() method to retrieve an external resource by the resource path.

1. Spring Resource Interface

The Spring Resource is a general interface for representing an external resource. Spring provides the following 6 implementations for the Resource interface.

  • UrlResource
  • ClassPathResource
  • FileSystemResource
  • ServletContextResource
  • InputStreamResource
  • ByteArrayResource

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

PrefixExampleExplanation
classpath:classpath:com/myapp/config.xmlLoaded from the classpath.
file:file:///data/config.xmlLoaded as a URL from the filesystem.
http:https://myserver/logo.pngLoaded as a URL.
(none)/data/config.xmlDepends on the underlying ApplicationContext.

2. Loading a Resource using ResourceLoader

The ResourceLoader is used for loading resources from classpath or file system resources. It has two methods:

//Expose the ClassLoader used by this ResourceLoader.
ClassLoader getClassLoader()

//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. To get the reference of ResourceLoader, implement the ResourceLoaderAware interface.

@Component
public class ResourceUtils implements ResourceLoaderAware {
	
	private ResourceLoader resourceLoader;

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

	public Resource readMetadataFile() {
		Resource file = resourceLoader.getResource("file:c:/temp/fileSystemData.txt");;
		return file;
	}
}

3. Loading a Resource using ApplicationContext

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

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

@Component
public class ResourceUtils implements ApplicationContextAware {
	
	private ApplicationContext applicationContext;

	public void setResourceLoader(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	public Resource readMetadataFile() {
		Resource file = applicationContext.getResource("file:c:/temp/fileSystemData.txt");;
		return file;
	}
}

4. Building Paths for Loading Resources

4.1. Load a Resource from Application Root Directory

To load files from the application’s root directory folder, use the below template:

Resource banner = resourceLoader.getResource("file:data.txt");

4.2. Load a Resource from Class Path

To load a file from the classpath, use the below template:

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

4.3. Load a Resource from the Filesystem

To load files from the filesystem outside the application folder, use the absolute path of the file:

Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");

4.4. Load Resource from URL

To load files from any URL, use the below template:

Resource banner = resourceLoader.getResource("//howtodoinjava.com/readme.txt");

5. Conclusion

The Spring tutorial taught us to read a file or resource using the ResourceLoader and ApplicationContext interfaces. We learned to read the files from various sources, such as the application root folder, URL or an external file.

Happy Learning !!

Leave a Reply

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial