SPring的资源管理

  • 目标
    • 了解资源表示形式
    • 了解资源访问

  • SPring的资源管理在JDK的基础功能上进行了强大的拓展
    • 隐藏底层实现
    • 新增资源存在判断、资源操作权限相关的功能,相对于java.net.URL资源不存在则设置好为null更友好
    • 支持通配符获取资源

  • Spring管理资源(如下):
    • UrlResource
    • ClassPathResource
    • FileSystemResource
    • ServletContextResource
    • InputStreamResource
    • ByreArrayResource​​​​​​​

  • 资源协议与路径

  • Spring使用Resource接口,访问底层资源:
public interface Resource extends InputStreamSource{
    boolean exists();
    boolean isOpen();
    
    URL getURL() throows IOException;
    File getFile() throws IOException;
    Resource createRelative(String relativePath) throws IOException;
    String getFilename();
    String getDEscription();

}

public interface InputStreamSource(){
    InputStream getInputStream() throws IOEception;
}

  • 使用ResourceLoader接口记载资源


  • 应用上下文与资源
    • ​​​​​​​从classpath中加载资源
  • ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    •  从当前程序运行的工作目录,用相对路径加载资源
    • 使用指定前缀,从classpath中加载资源
    • 使用通配符 * 加载资源

  • Mybatis配置映射文件时使用的资源串与Spring相似:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值