Spring 框架文档之核心技术—— Resource

简介: Spring 通过 Resource 对底层资源进行统一访问。 其实现类有 ClassPathResource、FileSystemResource、UrlResource、ByteArrayResource、ServletContextResource 和 InputStreamResource。

Resource

Resource 接口定义

public interface Resource extends InputStreamSource {
    /**
     * 返回此资源的 URL 句柄
     */
    URL getURL() throws IOException;

    /**
     * 返回此资源的 URI 句柄
     */
    URI getURI() throws IOException;

    /**
     * 返回此资源的 File 句柄
     */
    File getFile() throws IOException;

    /**
     * 创建此资源的关联资源
     */
    Resource createRelative(String relativePath) throws IOException;
}

public interface InputStreamSource {
    /**
     * 返回底层资源内容的 InputStream
     */
    InputStream getInputStream() throws IOException;
}

内置 Resource 实现者

  • UrlResource —— 封装 java.net.URL,用于访问通过URL访问的任何对象,例如 file:http:ftp:
  • ClassPathResource —— 使用线程上下文类加载器、给定的类加载器或给定的类从类路径加载资源。如 classpath:
  • FileSystemResource —— 从文件系统加载资源
  • ServletContextResource —— 从 Web 应用根目录的相对路径中加载资源,支持 URL、流、File(资源位于文件系统)访问
  • InputStreamResource —— 在没有特定的资源实现时使用
  • ByteArrayResource —— 从任何给定的字节数组加载资源

ResourceLoader

  • 返回资源类型确认
//对于 ClassPathXmlApplicationContext, 返回 ClassPathResource
//对于 FileSystemXmlApplicationContext, 返回 FileSystemResource
//对于 WebApplicationContext, 返回 ServletContextResource
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");

//指定 classpath:,返回 ClassPathResource
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");

//指定 file:、http:,返回 UrlResource
Resource template = ctx.getResource("file:///some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("https://myhost.com/resource/path/myTemplate.txt");

与 ApplicationContext 协作

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

//注意:FileSystemXmlApplicationContext 强制将 FileSystemResource 路径当作相对路径
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");

//如要使用绝对路径,推荐使用 file: 形式的 UrlResource
ApplicationContext ctx = new FileSystemXmlApplicationContext("file:///conf/context.xml");
相关文章
|
2月前
|
Java Spring
聊聊你对SpringBoot框架的理解 ?
SpringBoot是Spring家族中流行的子项目,旨在简化Spring框架开发的繁琐配置。它主要提供三大功能:starter起步依赖简化依赖管理,自动配置根据条件创建Bean,以及内嵌Web服务器支持Jar包运行,极大提升了开发效率。
115 0
|
2月前
|
缓存 安全 Java
Spring 框架核心原理与实践解析
本文详解 Spring 框架核心知识,包括 IOC(容器管理对象)与 DI(容器注入依赖),以及通过注解(如 @Service、@Autowired)声明 Bean 和注入依赖的方式。阐述了 Bean 的线程安全(默认单例可能有安全问题,需业务避免共享状态或设为 prototype)、作用域(@Scope 注解,常用 singleton、prototype 等)及完整生命周期(实例化、依赖注入、初始化、销毁等步骤)。 解析了循环依赖的解决机制(三级缓存)、AOP 的概念(公共逻辑抽为切面)、底层动态代理(JDK 与 Cglib 的区别)及项目应用(如日志记录)。介绍了事务的实现(基于 AOP
|
2月前
|
存储 缓存 NoSQL
Spring Cache缓存框架
Spring Cache是Spring体系下的标准化缓存框架,支持多种缓存(如Redis、EhCache、Caffeine),可独立或组合使用。其优势包括平滑迁移、注解与编程两种使用方式,以及高度解耦和灵活管理。通过动态代理实现缓存操作,适用于不同业务场景。
254 0
|
2月前
|
消息中间件 NoSQL Java
SpringBoot框架常见的starter你都用过哪些 ?
本节介绍常见的Spring Boot Starter,分为官方(如Web、AOP、Redis等)与第三方(如MyBatis、MyBatis Plus)两类,用于快速集成Web开发、数据库、消息队列等功能。
197 0
|
2月前
|
缓存 安全 Java
第五章 Spring框架
第五章 Spring框架
|
Java 开发者 容器
《Spring技术内幕》——第一部分
本节书摘来自华章社区《Spring技术内幕》一书中的第一部分,作者:计文柯,更多章节内容可以访问云栖社区“华章社区”公众号查看
1500 0
|
Java Spring
《Spring技术内幕》——1.5节小结
本节书摘来自华章社区《Spring技术内幕》一书中的第1章,第1.5节小结,作者:计文柯,更多章节内容可以访问云栖社区“华章社区”公众号查看
1157 0
|
2月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
708 0
|
6月前
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
289 0
|
2月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
353 0
第07课:Spring Boot集成Thymeleaf模板引擎