“This is the 27th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
Unified Resource Resource
Resource is an abstraction and access interface for all resources of the Spring framework. It defines some common methods and provides a unified implementation through subclass AbstractResource
Subclasses structure
-
FileSystemResource: Encapsulates resources of the File type
-
ByteArrayResource: Encapsulates the data provided by a byte array.
-
UrlResource: Encapsulates resources of URL type
-
ClassPathResource: Implementation of resources of class path type
-
InputStreamResource: A Resource implementation class that treats the given InputStream as a Resource
AbstractResource
Resource interface default implementation, implementation of the Resource interface most of the public implementation
Unified Resource locating ResourceLoader
Spring separates Resource definition from Resource loading. Resource defines unified resources, and Resource loading is defined by ResourceLoader.
ResourceLoader is a unified abstraction of Resource loading. Specific Resource loading is realized by subclasses. ResourceLoader can be called a unified Resource locator, which is mainly used to return corresponding resources according to a given Resource file.
public interface ResourceLoader {
String CLASSPATH_URL_PREFIX = "classpath:";
Resource getResource(String var1);
@Nullable
ClassLoader getClassLoader();
}
Copy the code
- getResource
Returns the Resource instance based on the provided Resource path location. This method supports resources such as URLS,classpath, and relative paths
- getClassLoader
Return the classLoader instance.
Subclasses structure
DefaultResourceLoader
Similar to AbstractResource, is the default implementation of ResourceLoader
The constructor
It takes a ClassLoader as an argument to the constructor, which takes no arguments and uses the default ClassLoader
GetSource method
GetSource method is the most core ResourceLoader method, DefaultResourceLoader provides the core implementation of this method.
This method first returns the appropriate Resource based on the location provided.
-
First load the Resource through the ProtocolResolver, return Resource on success.
-
Second, it will determine the specific type of lLocation and return the corresponding Resource.
ProtocolResolver
User-defined protocol resource resolution policy that allows users to customize resource loading protocols without the need for a base ResourceLoader subclass.
ProtocolResolver Interface. Only one method Resource Resolve (String location, ResourceLoader ResourceLoader).
Resource Loading Example
public class ResourceTest { public static void main(String[] args) { ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource fileResource1 = resourceLoader.getResource("E:/k.txt"); System.out.println("fileResource1 is FileSystemResource:" + (fileResource1 instanceof FileSystemResource)); Resource fileResource2 = resourceLoader.getResource("/Users/chenming673/Documents/spark.txt"); System.out.println("fileResource2 is ClassPathResource:" + (fileResource2 instanceof ClassPathResource)); Resource urlResource1 = resourceLoader.getResource("file:/Users/chenming673/Documents/spark.txt"); System.out.println("urlResource1 is UrlResource:" + (urlResource1 instanceof UrlResource)); Resource urlResource2 = resourceLoader.getResource("http://www.baidu.com"); System.out.println("urlResource1 is urlResource:" + (urlResource2 instanceof UrlResource)); }} ---- Result -- fileResource1 is FileSystemResource:false fileResource2 is ClassPathResource:true urlResource1 is UrlResource:true urlResource1 is urlResource:trueCopy the code
For fileResource1, false is returned because it is of type ClassPathResource
We know the “E:/k.txt” address, but there is no resource type in this method, so it will pass DefaultResourceLoader#getResourceByPath(…) when throwing a MalformedURLException. Method to construct a resource of type ClassPathResource.
FileSystemResourceLoader
DefaultResourceLoader is inherited, and the getResourceByPath(String) method is overridden
Used to load file system resources and return FileSystemResource type.
FileSystemContextResource for FileSystemResourceLoader inner classes, it inherits FileSystemResource class, realize ContextResource interface
ClassRelativeResourceLoader
Similar to FileSystemResourceLoader, the getResourceByPath () method is overridden, but it can load resources based on a given class package or wrapper.
ResourcePatternResolver
It supports returning multiple Resource instances at a time based on the specified Resource path matching pattern.
conclusion
-
Spring provides Resource and ResourceLoader to uniformly abstract the entire Resource and its location. It makes the resource and resource positioning have a clearer boundary, and provides a suitable Default class, making the custom implementation more convenient and clear.
-
AbstractResource is the default abstract implementation of Resource. AbstractResource provides a unified implementation of the Resource interface. Subclasses of this class only need to override the corresponding methods.
-
DefaultResourceLoader is also the default implementation of ResourceLoader. When customizing ResourceLoader, we can inherit this class and implement the ProtocolResolver interface to implement the customizing resource loading protocol.
-
DefaultResourceLoader can only return a single resource at a time, so Spring provides another interface for this, ResourcePatternResolver, This interface provides a policy for returning multiple resources based on the specified locationPattern. Its subclasses PathMatchingResourcePatternResolver is a comprehensive expression of ResourceLoader, because it is realized the Resource getResource (String location) method, The Resource[] getResources(String locationPattern) method is also implemented.