Spring获取资源方式

简介:

1.1.1.1.ClassPathXmlApplicationContext

ClassPathXmlApplicationContext支持从classpath中查找资源。假如我以下面的方式启动Spring

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

那么系统将会从classpath中查找myFile.xml文件。相当于:

URL resource = getClassLoader().getResource("myFile.xml");

1.1.1.2.FileSystemXmlApplicationContext

FileSystemXmlApplicationContext支持从文件系统中查找资源。假如我以下面的方式启动Spring

ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");

那么系统将会在文件系统中查找myFile.xml文件。相当于:

File resource = new File("myFile.xml");

1.1.1.3.XmlWebApplicationContext

XmlWebApplicationContext支持从webapp上下文中(也就是ServletContext对象中)查找资源。假如我以下面的方式启动Spring

ApplicationContext context = new XmlWebApplicationContext();

或者在/WEB-INF/web.xml中添加如下配置:

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/beans.xml</param-value>

</context-param>

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

那么系统将会在Web应用的根目录中查找myFile.xml。相当于:

URL resource = servletContext.getResource("myFile.xml");

1.1.1.4.ClasspathClasspath*前缀

除了用ClassPathXmlApplicationContext来装载classpath下的资源,所有的Spring ApplicationContext实现都支持从classpath中装载资源:

l  使用classpath前缀:“classpath:myFile.xml —— classpath中查找资源myFile.xml

l  使用classpath*前缀:“classpath*:/META-INF/my*.xml —— classpath中查找所有符合pattern的资源。

1.1.2.Spring ResourceLoader的缺点

l  鱼和熊掌不可得兼

Spring ResourceLoader是由ApplicationContext来实现的。而你一次只能选择一种ApplicationContext的实现 —— 如果你选择了XmlWebApplicationContext,你就放弃了FileSystemXmlApplicationContext;反之亦然。

WEB应用中,由于Spring使用了XmlWebApplicationContext,因此你就无法装载文件系统下的资源。

l  不透明性

你必须用“绝对路径”来引用Spring中的资源。

假如你使用FileSystemXmlApplicationContext来访问资源,你必须使用绝对路径来访问文件或目录资源。这妨碍了应用程序在不同系统中布署的自由。因为在不同的系统中,例如WindowsLinux,文件的绝对路径是不同的。为了系统管理的需要,有时也需要将文件或目录放在不同于开发环境的地方。

即便是访问WEB应用下的资源,或者是classpath下的资源,你也必须明确指出它们的位置,例如:WEB-INF/myFile.xmlclasspath:myFile.xml等。如果我希望把classpath:myFile.xml挪到另一个物理位置,就必须修改所有的引用。

l  无扩展性

我无法在Spring ResourceLoader机制中增加一种新的装载资源的方法。例如,我希望把资源文件保存在数据库中,并用ResourceLoader来取得它。

 


本文转自 zhouhaipeng 51CTO博客,原文链接:http://blog.51cto.com/tianya23/695352,如需转载请自行联系原作者


相关文章
|
3月前
|
安全 Java Spring
Spring Security OAuth 资源服务器认证浅析
【1月更文挑战第18天】之前的几篇文章介绍了如何进行用户认证并向客户端分发 Token,以及 Token 是如何生成的,这篇分析一下,当客户端拿着 Token 去访问资源的时候,资源服务器是如何验证 Token 是否合法的。
51 2
|
4月前
|
Java 数据库 Spring
【Spring】资源操作管理:Resource、ResourceLoader、ResourceLoaderAware;
【Spring】资源操作管理:Resource、ResourceLoader、ResourceLoaderAware;
46 1
|
5月前
|
缓存 安全 算法
Spring Security OAuth 2.0 资源服务器— JWT
Spring Security OAuth 2.0 资源服务器— JWT
228 1
|
2月前
|
存储 Java Maven
SpringCloud Oauth2.0 实现资源验证
SpringCloud Oauth2.0 实现资源验证
34 0
|
3月前
|
前端开发 Java Spring
Spring5源码(14)-Spring资源文件读取
Spring5源码(14)-Spring资源文件读取
25 1
|
3月前
|
XML Java 数据格式
spring之资源操作:Resources
【1月更文挑战第17天】 一、Spring Resources概述 二、Resource接口 三、Resource的实现类 1、UrlResource访问网络资源 2、ClassPathResource 访问类路径下资源 3、FileSystemResource 访问文件系统资源 4、ServletContextResource 5、InputStreamResource 6、ByteArrayResource 四、Resource类图 五、ResourceLoader 接口 1、ResourceLoader 概述 2、使用演示 3、ResourceLoader 总结 六、ResourceLo
70 1
|
3月前
|
XML Java 数据格式
Spring Resources资源操作
Spring Resources资源操作
|
5月前
|
设计模式 Java API
使用Spring框架创建一个RESTful API,实现学生信息的管理,包括资源的创建、读取、更新和删除。
在当今的Web应用程序开发中,RESTful API(Representational State Transferful Application Programming Interface)变得越来越重要。Spring框架提供了强大的工具和功能,以便轻松创建、读取、更新和删除(CRUD)资源。在这篇文章中,我们将深入探讨如何使用Spring框架创建一个RESTful API,并通过一个完整的示例演示。
|
5月前
|
存储 Java Spring
Spring框架中的Resource接口是什么,以及它在加载和访问资源时的关键作用
使用 Resource 加载资源 要使用 Resource 接口加载资源,首先需要获取一个 ResourceLoader 实例,通常可以通过依赖注入来获得。接下来,您可以使用 ResourceLoader 来获取 Resource 对象,然后使用它来访问资源的内容。
|
7月前
|
XML Java API
Spring基础教程—资源(Resources)
Spring基础教程—资源(Resources)
130 0

热门文章

最新文章