Resources interface and implementation class

Spring Resources Overview

Java’s standard iava.net.URL class and standard handlers for various URL prefixes cannot satisfy all access to low-evel resources, such as: There is no standardized URL implementation available for accessing resources that need to be obtained from the classpath or relative to the ServletContext . And it lacks some functions required by Spring, such as detecting whether a resource exists, etc. Spring’s Resource declares the ability to access low-level resources.

Resource interface

Spring’s Resource interface is located in org.springframework.core.io. Intended to be a more powerful interface for abstracting access to low-level resources. The following shows the methods defined by the Resource interface

public interface Resource extends InputStreamSource {
    boolean exist();
    
    boolean isReadable;
    
    boolean isOpen();
    
    boolean isFile();
    
    URL getURL() throws IOException;
    
    URL getURI() throws IOException;
    
    File getFile() throws IOException;
    
    ReadableByteChannel readableChannel() throws IOException;
    
    Resource createRelative(String relativePath) throws IOException;
    
    String getFilename();
    
    String getDescription();
    
}

UrlResource accesses network resources

1. Create module

spring6-resource

2.Introduce dependencies

 <dependencies>

        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!-- spring context dependency-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.2</version>
        </dependency>

<!-- spring-aop dependency-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>6.0.2</version>
        </dependency>
<!-- spring aspects dependency-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>6.0.2</version>
        </dependency>
        <!-- spring integrates Junit related dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>6.0.2</version>
        </dependency>

        <!-- junit-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>

        <!-- log4j dependency -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <version>2.19.0</version>
        </dependency>


    </dependencies>

3. Create package

com.yogurt.spring6.resource

4. Create classes

UrlResourceDemo (demonstrates UrlResource accessing network resources)

//UrlResource access network resources
public class UrlResourceDemo {
    public static void main(String[] args) {
        //Access prefix http
        loadUrlResource("http://www.baidu.com");
        //Access prefix file
        loadUrlResource("file:yogurt.txt");
    }

    //Access prefix http, file
    public static void loadUrlResource(String path){
        try {
        //Create the object UrlResource of the Resource implementation class
            UrlResource url = new UrlResource(path);
            //Get resource information
            System.out.println(url.getFilename());
            System.out.println(url.getURI());
            System.out.println(url.getDescription());
            System.out.println(url.getInputStream().read());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Note: The yogart.txt file must be accessed in the root path.

ClassPathResourceDemo (demonstrates ClassPathResource access to resources under the class path)

//Access resources under the class path
public class ClassPathResourceDemo {

    public static void loadClasspathResource(String path){
        //Create object ClassPathResource
        ClassPathResource resource = new ClassPathResource(path);

        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
        //Get file content
        try {
            InputStream in = resource.getInputStream();
            byte[] b = new byte[1024];
            while (in.read(b) != -1){
                System.out.println(new String(b));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        loadClasspathResource("yogurt.txt");
    }
}

FileSystemResourceDemo (demonstrates FileSystemResource accessing resources under the class path)

//Access system resources
public class FileSystemResourceDemo {
    public static void main(String[] args) {
        //Absolute path (you must first create the yogart.txt file in the D drive)
        loadFileResource("d:\yogurt.txt");

        //relative path
        loadFileResource("yogurt.txt");
    }

    public static void loadFileResource(String path){
        //Create object
        FileSystemResource resource = new FileSystemResource(path);

        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
        try {
            InputStream in = resource.getInputStream();
            byte[] b = new byte[1024];
            while (in.read(b) != -1){
                System.out.println(new String(b));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Resource class circle

The relationship between the above Resource implementation class and the Resource top-level interface can be represented by the following UML relationship model