1. 程式人生 > 其它 >Spring Framework Doc - Resources

Spring Framework Doc - Resources

This chapter covers how Spring handles resources and how you can work with resources in Spring.

2.1. Introduction

Java’s standard java.net.URL class and standard handlers for various URL prefixes, unfortunately, are not quite adequate enough for all access to low-level resources. For example, there is no standardized URL implementation that may be used to access a resource that needs to be obtained from the classpath or relative to a ServletContext. While it is possible to register new handlers for specialized URL prefixes, similar to existing handlers for prefixes such as http, this is generally quite complicated, and the URL interface still lacks some desirable functionality, such as a method to check for the existence of the resource being pointed to.

2.2. The Resource Interface

Spring’s Resource interface is meant to be a more capable interface for abstracting access to low-level resources.

2.3. Built-in Resource Implementations

1、UrlResource:
2、ClassPathResource:
3、FileSystemResource
4、PathResource
5、ServletContextResource
6、InputStreamResource
7、ByteArrayResource

2.4. The ResourceLoader Interface

The ResourceLoader interface is meant to be implemented by objects that can return Resource instances.

2.5. The ResourcePatternResolver Interface

The ResourcePatternResolver interface is an extension to the ResourceLoader interface which defines a strategy for resolving a location pattern into Resource objects.

2.6. The ResourceLoaderAware Interface

The ResourceLoaderAware interface is a special callback interface which identifies components that expect to be provided a ResourceLoader reference.

2.7. Resources as Dependencies

If the bean itself is going to determine and supply the resource path through some sort of dynamic process, it probably makes sense for the bean to use the ResourceLoader or ResourcePatternResolver interface to load resources.

2.8. Application Contexts and Resource Paths

This section covers how to create application contexts with resources, including shortcuts that work with XML, how to use wildcards, and other details.