Spring study notes Resource Resources

Spring Resources


Outline

In the daily program development, deal with external resources is very complicated thing, we may need to deal with the URL resource, File resources, ClassPath related resources and so on. And java in Java .net.URL class and standard handlers for various URL prefixes are not sufficient for all access to the underlying resources. Resources to deal with a variety of needs to use a different interface, which increases the complexity of the system.

This Spring Resource interface provides a consistent unified these underlying resource access. Resource interface is a more powerful interface to abstract access to the underlying resources.

public interface Resource extends InputStreamSource {

    boolean exists();

    boolean isOpen();

    URL getURL() throws IOException;

    File getFile() throws IOException;

    Resource createRelative(String relativePath) throws IOException;

    String getFilename();

    String getDescription();

}

Resource inherited InputStreamSource following content is InputStreamSource

public interface InputStreamSource {
    InputStream getInputStream() throws IOException;
}

Analytical method :

  1. getInputStream (): Each call returns a stream corresponding to the resource InputStream byte, the caller is necessary to close the resource after use.
  2. exists (): Returns true if the current resource exists.
  3. isOpen (): Returns the representatives Resource resource has already been opened. If it returns true, it can only be read once InputStream can not be read many times, and then closed to avoid resource leaks. Resource implementation for all common general returns false.
  4. getURL (): returns the representative objects corresponding to the resources java.util.URL Resource.
  5. getFile (): returns the representative objects corresponding to the resources java.io.File Resource.
  6. createRelative (String relativePath): used to create a resource with the current Resource represents a resource, such as representatives of the current Resource File resources "d: / test /" is createRelative ( "test.txt") returns the resource table file "d: / test /test.txt "resource resources.
  7. getFilename (): Returns the Resource represents a resource corresponding to the file path.
  8. getDescription (): Returns the Resource represents a resource descriptor, usually the full path of the resource (the actual file name or the actual URL address).

Spring Framework itself is very widely used Resource, it can also be used in our project and can be very convenient to get to the target resource.

Built-in Resource interface

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

A UrlResource
a UrlResource encapsulates java.net.URL, can be used to access any object accessed through a URL, such as files, HTTP resources, FTP resources and so on. All have a URL normalized string representation, it is possible to use an appropriate standardized URL prefixes to indicate different types of URL. Generally supports the following resource access.

  1. file: path to access the file system
new UrlResource("file:d/xxx.txt");
  1. http: for accessing resources via the http protocol
new UrlResource("http://地址");
  1. ftp: ftp access resources through
new UrlResource("ftp://地址");

A ClassPathResource
a ClassPathResource retrieves the resource class path, it uses the thread context class loader, a given resource class loader to load. classpath classpath resources exist in the file system of the bag or jar.

ClassPathResource common constructor

public ClassPathResource(String path);
public ClassPathResource(String path, @Nullable ClassLoader classLoader);
public ClassPathResource(String path, @Nullable Class<?> clazz);
  1. public ClassPathResource (String path): the default class loader path described in the resource class path
  2. public ClassPathResource (String path, @Nullable ClassLoader classLoader): using the specified class loader path of the resource class path
  3. public ClassPathResource (String path, @Nullable Class clazz): only load path resources under the class with the specified class path

FileSystemResource
FileSystemResource is a Resource implementation supports java.io and java.nio.file.Path treatment.

ServletContextResource
ServletContextResource is a web application resources to achieve ServletContext resources. GetResourceAsStream ServletContext getResource operations and to simplify the operation of the interface servlet container.

InputStreamResource
InputStreamResource is based on the realization of InputStream, InputStream parameter is a use it only when there is no Resource under the specific scene.

public InputStreamResource(InputStream inputStream);
public InputStreamResource(InputStream inputStream, @Nullable String description);

Prefer ByteArrayResource
prefer ByteArrayResource is given a byte [realize] array to create ByteArrayInputStream.

ResourceLoader

Examples of Resource ResourceLoader returned.

public interface ResourceLoader {
    Resource getResource(String location);
}

Spring ApplicationContext implement the ResourceLoader

  1. ClassPathXmlApplicationContext
  2. FileSystemXmlApplicationContext
  3. WebApplicationContext

Can be used to access to resources ApplicationContext instance, when the parameter does not specify a resource in a prefix call getResource ApplicationContext implementation class will be returned for instance ApplicationContext types of resources, following execution code is ClassPathXmlApplicationContext

Resource template = ctx.getResource("some/resource/path/myTemplate.txt");

For a return code above ClassPathXmlApplicationContext ClassPathResource.
If the same method performed FileSystemXmlApplicationContext instance, it returns FileSystemResource. For WebApplicationContext, it will return a ServletContextResource.

Alternatively, you can specify the resource prefixes such as classpath: mandatory use of ClassPathResource no matter what ApplicationContext implementations instance of type.

Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");

You can also use other prefixes

Resource template = ctx.getResource("file:///some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("https://myhost.com/resource/path/myTemplate.txt");

getResource string parameter object into a resource object policy

Resource prefix Examples Explanation
classpath: classpath:com/myapp/config.xml Loaded from the classpath classpath
file: file:///data/config.xml Loaded as a URL from the file system
http: https://myserver/logo.png Loaded as a URL
(none) /data/config.xml The implementation example ApplicationContext loading

The ResourceLoaderAware
The ResourceLoaderAware callback interface is a special interface through a corresponding ApplicationContext injection.

public interface ResourceLoaderAware {
    void setResourceLoader(ResourceLoader resourceLoader);
}

When a class implements an interface ResourceLoaderAware, Spring IoC container when loading the bean type of the bean is recognized as ResourceLoaderAware then calls the corresponding instance setResourceLoader (ResourceLoader resourceLoader) ApplicationContext method and returns an instance of the method of passing in getResource

ApplicationContext building

ApplicationContext construct typically a string or array of strings as the location path of the resource. When the path is not a prefix string parameter, loading the bean Resource instances depends on the instance of the ApplicationContext. For example, consider the following example, which creates a ClassPathXmlApplicationContext:

ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");

appContext.xml documents in the bean are loaded from the class path, because of the use Classpathresource.

ApplicationContext build wildcard
building parameters precise path ApplicationContext can be the path to classpath: conf / appContext.xml each path has to target resource-one mapping. Or it can contain special characters classpath *: One use of this mechanism is that when you need to rely on style application assembly. And when used in the classpath *: the same path as the prefix to create the final ApplicationContext, all dependencies are automatically acquired classpath.

classpath, and classpath * difference

  1. classpath: for loading one and only one resource class path (including jar package) in the; return for only a plurality of matching.
  2. classpath *: for all resources to match loading class path (including jar package) was added.

Note: The classpath *: need to traverse all the classpath, so load efficiency will be relatively worse as little as possible to use.

Guess you like

Origin blog.csdn.net/qq_16830879/article/details/91876712