Spring5中,如何解析Resource与ResourceLoader接口?

2026-05-21 02:134阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计948个文字,预计阅读时间需要4分钟。

Spring5中,如何解析Resource与ResourceLoader接口?

Resource接口位于org.springframework.core.io包下;Spring定义Resource接口是为了提供更强的底层资源访问能力;对于Spring来说,Resource接口代表着物理存在的任何资源。

Resource

  • Spring的Resource接口位于包org.springframework.core.io中;

  • Spring定义Resource接口是为了提供更强的访问底层资源能力的抽象;

  • 对spring来说Resource接口代表着物理存在的任何资源。

其中,最常用的有四个:

  • ClassPathResource:通过 ClassPathResource 以类路径的方式进行访问。
  • FileSystemResource:通过 FileSystemResource 以文件系统绝对路径的方式进行访问。
  • ServletContextResource:通过 ServletContextResource 以相对于Web应用根目录的方式进行访问。
  • UrlResource:通过java.net.URL来访问资源,当然它也支持File格式,如“file:”。

Resource接口

public interface Resource extends InputStreamSource { //判断资源是否存在 boolean exists(); //判断资源的内容是否可读 default boolean isReadable() { return exists(); } //判断当前Resource代表的底层资源是否已经打开 default boolean isOpen() { return false; } //判断是否是文件系统的文件 default boolean isFile() { return false; } //返回当前资源对应的URL URL getURL() throws IOException; //返回当前资源对应的URI URI getURI() throws IOException; //返回当前资源对应的File File getFile() throws IOException; default ReadableByteChannel readableChannel() throws IOException { return Channels.newChannel(getInputStream()); } //返回当前资源的长度 long contentLength() throws IOException; //返回当前Resource代表的底层资源的最后修改时间 long lastModified() throws IOException; //根据资源的相对路径创建新资源 Resource createRelative(String relativePath) throws IOException; //获取资源的文件名 @Nullable String getFilename(); //返回当前资源的底层资源的描述符 String getDescription(); //获取当前资源代表的输入流 InputStream getInputStream() throws IOException; }

它是spring访问资源最基本的接口。实际访问的时候直接用Resource接口就可以,不必使用其子类。其实经常用到的(resource的真正目的)方法是public InputStream getInputStream()。

FileSystemResource

public class FileSystemResource extends AbstractResource implements WritableResource { private final String path; @Nullable private final File file; private final Path filePath; public FileSystemResource(String path) { Assert.notNull(path, "Path must not be null"); this.path = StringUtils.cleanPath(path); this.file = new File(path); this.filePath = this.file.toPath(); } @Override public InputStream getInputStream() throws IOException { try { return Files.newInputStream(this.filePath); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } } }

这里的path一般要给出绝对路径,当然也可以是相对路径,如果是相对路径要注意其根目录。

ClassPathResource

public class ClassPathResource extends AbstractFileResolvingResource { private final String path; @Nullable private ClassLoader classLoader; @Nullable private Class<?> clazz; public ClassPathResource(String path, @Nullable ClassLoader classLoader) { Assert.notNull(path, "Path must not be null"); String pathToUse = StringUtils.cleanPath(path); if (pathToUse.startsWith("/")) { pathToUse = pathToUse.substring(1); } this.path = pathToUse; this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); } @Override public InputStream getInputStream() throws IOException { InputStream is; if (this.clazz != null) { is = this.clazz.getResourceAsStream(this.path); } else if (this.classLoader != null) { is = this.classLoader.getResourceAsStream(this.path); } else { is = ClassLoader.getSystemResourceAsStream(this.path); } if (is == null) { throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist"); } return is; } }

这里是通过Class或者ClassLoader的getResourceAsStream()方法来获得InputStream的。其path一般都是以“classpath:”开头,如果以“classpath*:”开头表示所有与给定名称匹配的classpath资源都应该被获取。

ServletContextResource

public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource { private final ServletContext servletContext; private final String path; public ServletContextResource(ServletContext servletContext, String path) { // check ServletContext Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext"); this.servletContext = servletContext; // check path Assert.notNull(path, "Path is required"); String pathToUse = StringUtils.cleanPath(path); if (!pathToUse.startsWith("/")) { pathToUse = "/" + pathToUse; } this.path = pathToUse; } @Override public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; } }

ServletContextResource通过ServletContext的getResourceAsStream()来取得InputStream,这里path必须以“/”开头,并且相对于当前上下文的根目录。如常用的path="/WEB-INF/web.xml"。

UrlResource

public class UrlResource extends AbstractFileResolvingResource { @Nullable private final URI uri; private final URL url; private final URL cleanedUrl; public UrlResource(URI uri) throws MalformedURLException { Assert.notNull(uri, "URI must not be null"); this.uri = uri; this.url = uri.toURL(); this.cleanedUrl = getCleanedUrl(this.url, uri.toString()); } @Override public InputStream getInputStream() throws IOException { URLConnection con = this.url.openConnection(); ResourceUtils.useCachesIfNecessary(con); try { return con.getInputStream(); } catch (IOException ex) { // Close the HTTP connection (if applicable). if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } } }

UrlResource 封装了java.net.URL,它能够被用来访问任何通过URL可以获得的对象,例如:文件、HTTP对象、FTP对象等。

 

Spring5中,如何解析Resource与ResourceLoader接口?

所有的URL都有个标准的 String表示,这些标准前缀可以标识不同的URL类型,包括file:访问文件系统路径,blog.csdn.net/shadow_zed/article/details/72540927

www.cnblogs.com/jixp/articles/10702486.html

本文共计948个文字,预计阅读时间需要4分钟。

Spring5中,如何解析Resource与ResourceLoader接口?

Resource接口位于org.springframework.core.io包下;Spring定义Resource接口是为了提供更强的底层资源访问能力;对于Spring来说,Resource接口代表着物理存在的任何资源。

Resource

  • Spring的Resource接口位于包org.springframework.core.io中;

  • Spring定义Resource接口是为了提供更强的访问底层资源能力的抽象;

  • 对spring来说Resource接口代表着物理存在的任何资源。

其中,最常用的有四个:

  • ClassPathResource:通过 ClassPathResource 以类路径的方式进行访问。
  • FileSystemResource:通过 FileSystemResource 以文件系统绝对路径的方式进行访问。
  • ServletContextResource:通过 ServletContextResource 以相对于Web应用根目录的方式进行访问。
  • UrlResource:通过java.net.URL来访问资源,当然它也支持File格式,如“file:”。

Resource接口

public interface Resource extends InputStreamSource { //判断资源是否存在 boolean exists(); //判断资源的内容是否可读 default boolean isReadable() { return exists(); } //判断当前Resource代表的底层资源是否已经打开 default boolean isOpen() { return false; } //判断是否是文件系统的文件 default boolean isFile() { return false; } //返回当前资源对应的URL URL getURL() throws IOException; //返回当前资源对应的URI URI getURI() throws IOException; //返回当前资源对应的File File getFile() throws IOException; default ReadableByteChannel readableChannel() throws IOException { return Channels.newChannel(getInputStream()); } //返回当前资源的长度 long contentLength() throws IOException; //返回当前Resource代表的底层资源的最后修改时间 long lastModified() throws IOException; //根据资源的相对路径创建新资源 Resource createRelative(String relativePath) throws IOException; //获取资源的文件名 @Nullable String getFilename(); //返回当前资源的底层资源的描述符 String getDescription(); //获取当前资源代表的输入流 InputStream getInputStream() throws IOException; }

它是spring访问资源最基本的接口。实际访问的时候直接用Resource接口就可以,不必使用其子类。其实经常用到的(resource的真正目的)方法是public InputStream getInputStream()。

FileSystemResource

public class FileSystemResource extends AbstractResource implements WritableResource { private final String path; @Nullable private final File file; private final Path filePath; public FileSystemResource(String path) { Assert.notNull(path, "Path must not be null"); this.path = StringUtils.cleanPath(path); this.file = new File(path); this.filePath = this.file.toPath(); } @Override public InputStream getInputStream() throws IOException { try { return Files.newInputStream(this.filePath); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } } }

这里的path一般要给出绝对路径,当然也可以是相对路径,如果是相对路径要注意其根目录。

ClassPathResource

public class ClassPathResource extends AbstractFileResolvingResource { private final String path; @Nullable private ClassLoader classLoader; @Nullable private Class<?> clazz; public ClassPathResource(String path, @Nullable ClassLoader classLoader) { Assert.notNull(path, "Path must not be null"); String pathToUse = StringUtils.cleanPath(path); if (pathToUse.startsWith("/")) { pathToUse = pathToUse.substring(1); } this.path = pathToUse; this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); } @Override public InputStream getInputStream() throws IOException { InputStream is; if (this.clazz != null) { is = this.clazz.getResourceAsStream(this.path); } else if (this.classLoader != null) { is = this.classLoader.getResourceAsStream(this.path); } else { is = ClassLoader.getSystemResourceAsStream(this.path); } if (is == null) { throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist"); } return is; } }

这里是通过Class或者ClassLoader的getResourceAsStream()方法来获得InputStream的。其path一般都是以“classpath:”开头,如果以“classpath*:”开头表示所有与给定名称匹配的classpath资源都应该被获取。

ServletContextResource

public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource { private final ServletContext servletContext; private final String path; public ServletContextResource(ServletContext servletContext, String path) { // check ServletContext Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext"); this.servletContext = servletContext; // check path Assert.notNull(path, "Path is required"); String pathToUse = StringUtils.cleanPath(path); if (!pathToUse.startsWith("/")) { pathToUse = "/" + pathToUse; } this.path = pathToUse; } @Override public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; } }

ServletContextResource通过ServletContext的getResourceAsStream()来取得InputStream,这里path必须以“/”开头,并且相对于当前上下文的根目录。如常用的path="/WEB-INF/web.xml"。

UrlResource

public class UrlResource extends AbstractFileResolvingResource { @Nullable private final URI uri; private final URL url; private final URL cleanedUrl; public UrlResource(URI uri) throws MalformedURLException { Assert.notNull(uri, "URI must not be null"); this.uri = uri; this.url = uri.toURL(); this.cleanedUrl = getCleanedUrl(this.url, uri.toString()); } @Override public InputStream getInputStream() throws IOException { URLConnection con = this.url.openConnection(); ResourceUtils.useCachesIfNecessary(con); try { return con.getInputStream(); } catch (IOException ex) { // Close the HTTP connection (if applicable). if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } } }

UrlResource 封装了java.net.URL,它能够被用来访问任何通过URL可以获得的对象,例如:文件、HTTP对象、FTP对象等。

 

Spring5中,如何解析Resource与ResourceLoader接口?

所有的URL都有个标准的 String表示,这些标准前缀可以标识不同的URL类型,包括file:访问文件系统路径,blog.csdn.net/shadow_zed/article/details/72540927

www.cnblogs.com/jixp/articles/10702486.html