一、配置文件读取结构
spring对其内部使用到的资源实现了自己的抽象结构。该结构如下:
spring使用(接口-抽象类-实现类-内部类)的模式实现各种类型文件的读取。
InputStreamSource作为其顶级接口,只定义一个方法:
InputStream getInputStream() throws IOException;
Resource接口抽象了所有Spring内部使用到的底层资源。对于不同来源的资源文件都有相应的Resource实现:
boolean exists();
boolean isReadable();
boolean isOpen();
URL getURL() throws IOException;
URI getURI() throws IOException;
File getFile() throws IOException;
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDescription();
①文件:FileSystemResource
②class:CalssPathResource
③URL资源:UrlResource
④InputStream资源:InputStream
…
其中ServletContextResource来自Spring web模块。
ContextResource接口
其中只有定义了一个方法,获取相对与Context下的路径:
/**
* Return the path within the enclosing 'context'.
* <p>This is typically path relative to a context-specific root directory,
* e.g. a ServletContext root or a PortletContext root.
*/
String getPathWithinContext();
其中classPathContextResource、ClassRelativeContextResource、FileSystemContextResource均是内部类实现,ServletContextResource
是单独实现。
本文介绍了Spring框架中资源文件的加载机制。Spring通过多种Resource实现来支持不同来源的资源文件加载,如FileSystemResource、ClassPathResource等,并提供了统一的接口进行资源操作。
749

被折叠的 条评论
为什么被折叠?



