Close

Spring - Resource Loading

[Last Updated: Nov 7, 2021]

In this tutorial we will learn how to access resources (file/web resources).

Spring provides an unified resource access mechanism via interface ResourceLoader.

ResourceLoader is a super interface of ApplicationContext. We can access resources using application context instance or we can inject ResourceLoader as a bean or we can even use it outside of Spring container.

Definition of ResourceLoader

(Version: spring-framework 5.3.12)
package org.springframework.core.io;
   ........
public interface ResourceLoader {
    .............
    Resource getResource(String location); 1
    .............
    ClassLoader getClassLoader(); 2
}
1Return a Resource handle for the specified resource location.
The location can be one of the followings:
  • fully qualified URL, e.g. "file:C:/test.txt", "url:http://www.example.com"
  • a location in the classpath, e.g. "classpath:test.txt
  • a relative file paths, e.g. "WEB-INF/test.txt"
2Return the ClassLoader used by this ResourceLoader.

The Resource interface

It is a resource descriptor that abstracts from the actual type of underlying resource, such as a file or class path resource.

Definition of Resource

(Version: spring-framework 5.3.12)
package org.springframework.core.io;
   ........
public interface Resource extends InputStreamSource {
    boolean exists(); 
    boolean isReadable(); 
    boolean isOpen(); 
    boolean isFile(); 
    URL getURL() throws IOException; 
    URI getURI() throws IOException; 
    File getFile() throws IOException; 
    ReadableByteChannel readableChannel() throws IOException; 
    long contentLength() throws IOException; 
    long lastModified() throws IOException; 
    Resource createRelative(String relativePath) throws IOException; 
    .............
    String getFilename(); 
    String getDescription(); 
}

Resource interface extends InputStreamSource which has a method to get an instance of java.io.InputStream to read the resource data.

Definition of InputStreamSource

(Version: spring-framework 5.3.12)
package org.springframework.core.io;
   ........
public interface InputStreamSource {
    InputStream getInputStream() throws IOException; 
}

Implementations of Resource

There are multiple implementations of Resource interface: FileSystemResource, ClassPathResource, UrlResource.
The good thing is, we don't have to know about those implementations as our prefix based resource location triggers and initializes related implementation.

Example

In this example we will see how to use ResourceLoader and Resource outside of Spring context.

A test resource file

src/main/resources/myFile.txt

this is a my file content.

Loading via classpath

package com.logicbig.example;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import java.io.IOException;
import java.nio.file.Files;

public class ClasspathFileLoadingExample {
  public static void main(String[] args) throws IOException {
      ResourceLoader resourceLoader = new DefaultResourceLoader();
      Resource resource = resourceLoader.getResource("classpath:myFile.txt");
      byte[] bytes = Files.readAllBytes(resource.getFile().toPath());
      String fileContent = new String(bytes);
      System.out.println(fileContent);
  }
}

Output

this is a my file content.

Loading via absolute path

package com.logicbig.example;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import java.io.IOException;
import java.nio.file.Files;

public class FileUrlLoadingExample {
  public static void main(String[] args) throws IOException {
      ResourceLoader resourceLoader = new DefaultResourceLoader();
      Resource resource = resourceLoader.getResource("file:D:/test/myTestFile.txt");
      byte[] bytes = Files.readAllBytes(resource.getFile().toPath());
      String fileContent = new String(bytes);
      System.out.println(fileContent);
  }
}

Output

test file content

Loading a web resource

package com.logicbig.example;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HttpUrlLoading {
  public static void main(String[] args) throws IOException {
      ResourceLoader resourceLoader = new DefaultResourceLoader();
      Resource resource = resourceLoader.getResource("url:http://www.example.com");
      StringBuilder stringBuilder = new StringBuilder();
      try (BufferedReader reader = new BufferedReader(
              new InputStreamReader(resource.getInputStream()))) {
          reader.lines().forEach(stringBuilder::append);
      }
      System.out.println(stringBuilder.toString());
  }
}

Output

<!doctype html><html><head>    <title>Example Domain</title>    <meta charset="utf-8" />    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />    <meta name="viewport" content="width=device-width, initial-scale=1" />    <style type="text/css">    body {        background-color: #f0f0f2;        margin: 0;        padding: 0;        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;            }    div {        width: 600px;        margin: 5em auto;        padding: 2em;        background-color: #fdfdff;        border-radius: 0.5em;        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);    }    a:link, a:visited {        color: #38488f;        text-decoration: none;    }    @media (max-width: 700px) {        div {            margin: 0 auto;            width: auto;        }    }    </style>    </head><body><div>    <h1>Example Domain</h1>    <p>This domain is for use in illustrative examples in documents. You may use this    domain in literature without prior coordination or asking for permission.</p>    <p><a href="https://www.iana.org/domains/example">More information...</a></p></div></body></html>

In next tutorials we will see how to autowire ResourceLoader and Resource in Spring beans.

Example Project

Dependencies and Technologies Used:

  • spring-context 5.3.12 (Spring Context)
     Version Compatibility: 3.2.3.RELEASE - 5.3.12 Version List
    ×

    Version compatibilities of spring-context with this example:

    • 3.2.3.RELEASE
    • 3.2.4.RELEASE
    • 3.2.5.RELEASE
    • 3.2.6.RELEASE
    • 3.2.7.RELEASE
    • 3.2.8.RELEASE
    • 3.2.9.RELEASE
    • 3.2.10.RELEASE
    • 3.2.11.RELEASE
    • 3.2.12.RELEASE
    • 3.2.13.RELEASE
    • 3.2.14.RELEASE
    • 3.2.15.RELEASE
    • 3.2.16.RELEASE
    • 3.2.17.RELEASE
    • 3.2.18.RELEASE
    • 4.0.0.RELEASE
    • 4.0.1.RELEASE
    • 4.0.2.RELEASE
    • 4.0.3.RELEASE
    • 4.0.4.RELEASE
    • 4.0.5.RELEASE
    • 4.0.6.RELEASE
    • 4.0.7.RELEASE
    • 4.0.8.RELEASE
    • 4.0.9.RELEASE
    • 4.1.0.RELEASE
    • 4.1.1.RELEASE
    • 4.1.2.RELEASE
    • 4.1.3.RELEASE
    • 4.1.4.RELEASE
    • 4.1.5.RELEASE
    • 4.1.6.RELEASE
    • 4.1.7.RELEASE
    • 4.1.8.RELEASE
    • 4.1.9.RELEASE
    • 4.2.0.RELEASE
    • 4.2.1.RELEASE
    • 4.2.2.RELEASE
    • 4.2.3.RELEASE
    • 4.2.4.RELEASE
    • 4.2.5.RELEASE
    • 4.2.6.RELEASE
    • 4.2.7.RELEASE
    • 4.2.8.RELEASE
    • 4.2.9.RELEASE
    • 4.3.0.RELEASE
    • 4.3.1.RELEASE
    • 4.3.2.RELEASE
    • 4.3.3.RELEASE
    • 4.3.4.RELEASE
    • 4.3.5.RELEASE
    • 4.3.6.RELEASE
    • 4.3.7.RELEASE
    • 4.3.8.RELEASE
    • 4.3.9.RELEASE
    • 4.3.10.RELEASE
    • 4.3.11.RELEASE
    • 4.3.12.RELEASE
    • 4.3.13.RELEASE
    • 4.3.14.RELEASE
    • 4.3.15.RELEASE
    • 4.3.16.RELEASE
    • 4.3.17.RELEASE
    • 4.3.18.RELEASE
    • 4.3.19.RELEASE
    • 4.3.20.RELEASE
    • 4.3.21.RELEASE
    • 4.3.22.RELEASE
    • 4.3.23.RELEASE
    • 4.3.24.RELEASE
    • 4.3.25.RELEASE
    • 4.3.26.RELEASE
    • 4.3.27.RELEASE
    • 4.3.28.RELEASE
    • 4.3.29.RELEASE
    • 4.3.30.RELEASE
    • 5.0.0.RELEASE
    • 5.0.1.RELEASE
    • 5.0.2.RELEASE
    • 5.0.3.RELEASE
    • 5.0.4.RELEASE
    • 5.0.5.RELEASE
    • 5.0.6.RELEASE
    • 5.0.7.RELEASE
    • 5.0.8.RELEASE
    • 5.0.9.RELEASE
    • 5.0.10.RELEASE
    • 5.0.11.RELEASE
    • 5.0.12.RELEASE
    • 5.0.13.RELEASE
    • 5.0.14.RELEASE
    • 5.0.15.RELEASE
    • 5.0.16.RELEASE
    • 5.0.17.RELEASE
    • 5.0.18.RELEASE
    • 5.0.19.RELEASE
    • 5.0.20.RELEASE
    • 5.1.0.RELEASE
    • 5.1.1.RELEASE
    • 5.1.2.RELEASE
    • 5.1.3.RELEASE
    • 5.1.4.RELEASE
    • 5.1.5.RELEASE
    • 5.1.6.RELEASE
    • 5.1.7.RELEASE
    • 5.1.8.RELEASE
    • 5.1.9.RELEASE
    • 5.1.10.RELEASE
    • 5.1.11.RELEASE
    • 5.1.12.RELEASE
    • 5.1.13.RELEASE
    • 5.1.14.RELEASE
    • 5.1.15.RELEASE
    • 5.1.16.RELEASE
    • 5.1.17.RELEASE
    • 5.1.18.RELEASE
    • 5.1.19.RELEASE
    • 5.1.20.RELEASE
    • 5.2.0.RELEASE
    • 5.2.1.RELEASE
    • 5.2.2.RELEASE
    • 5.2.3.RELEASE
    • 5.2.4.RELEASE
    • 5.2.5.RELEASE
    • 5.2.6.RELEASE
    • 5.2.7.RELEASE
    • 5.2.8.RELEASE
    • 5.2.9.RELEASE
    • 5.2.10.RELEASE
    • 5.2.11.RELEASE
    • 5.2.12.RELEASE
    • 5.2.13.RELEASE
    • 5.2.14.RELEASE
    • 5.2.15.RELEASE
    • 5.2.16.RELEASE
    • 5.2.17.RELEASE
    • 5.2.18.RELEASE
    • 5.3.0
    • 5.3.1
    • 5.3.2
    • 5.3.3
    • 5.3.4
    • 5.3.5
    • 5.3.6
    • 5.3.7
    • 5.3.8
    • 5.3.9
    • 5.3.10
    • 5.3.11
    • 5.3.12

    Versions in green have been tested.

  • JDK 8
  • Maven 3.8.1

Spring - Resource Loading Basics Select All Download
  • spring-resource-loading
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ClasspathFileLoadingExample.java
          • resources

    See Also