Spring加载resource时classpath*:与classpath:的区别

Spring加载resource时classpath*:与classpath:的区别Spring 可以通过指定 classpath 与 classpath 前缀加路径的方式从 classpath 加载文件 如 bean 的定义文件 classpath 的出现是为了从多个 jar 文件中加载相同的文件 classpath 只能加载找到的第一个文件 比如 resource1 jar 中的 package com test rs 有一个 jarAppcontex xml 文件 内容如下

大家好,又见面了,我是你们的朋友全栈君。
如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Spring可以通过指定classpath*:与classpath:前缀加路径的方式从classpath加载文件,如bean的定义文件.classpath*:的出现是为了从多个jar文件中加载相同的文件.classpath:只能加载找到的第一个文件.

比如 resource1.jar中的package ‘com.test.rs’ 有一个 ‘jarAppcontext.xml’ 文件,内容如下:

<bean name=”ProcessorImplA” class=”com.test.spring.di.ProcessorImplA” />

resource2.jar中的package ‘com.test.rs’ 也有一个 ‘jarAppcontext.xml’ 文件,内容如下:

<bean id=”ProcessorImplB” class=”com.test.spring.di.ProcessorImplB” />

通过使用下面的代码则可以将两个jar包中的文件都加载进来

ApplicationContext ctx = new ClassPathXmlApplicationContext( “classpath*:com/test/rs/jarAppcontext.xml”);

而如果写成下面的代码,就只能找到其中的一个xml文件(顺序取决于jar包的加载顺序)

ApplicationContext ctx = new ClassPathXmlApplicationContext( “classpath:com/test/rs/jarAppcontext.xml”);

classpath*:的加载使用了classloader的 getResources() 方法,如果是在不同的J2EE服务器上运行,由于应用服务器提供自己的classloader实现,它们在处理jar文件时的行为也许会有所不同。 要测试classpath*: 是否有效,可以用classloader从classpath中的jar文件里加载文件来进行测试:getClass().getClassLoader().getResources("<someFileInsideTheJar>")。(上面的例子是在sun的jre中运行的状态)

 从Spring的源码,在PathMatchingResourcePatternResolver类中,我们可以更清楚的了解其对的处理:如果是以classpath*开头,它会遍历classpath.

 protected Resource[] findAllClassPathResources(String location) throws IOException { String path = location; if (path.startsWith("/")) { path = path.substring(1); } Enumeration resourceUrls = getClassLoader().getResources(path); Set<Resource> result = new LinkedHashSet<Resource>(16); while (resourceUrls.hasMoreElements()) { URL url = (URL) resourceUrls.nextElement(); result.add(convertClassLoaderURL(url)); } return result.toArray(new Resource[result.size()]); }

http://blog.csdn.net/kkdelta/article/details/,简介了在JAVA里遍历classpath中读取找到的所有符合名称的文件.

前缀 例子 说明

classpath:

classpath:com/myapp/config.xml

从classpath中加载。

file:

file:/data/config.xml

作为 URL 从文件系统中加载。

http:

http://myserver/logo.png

作为 URL 加载。

(none)

/data/config.xml

根据 ApplicationContext 进行判断。

从Spring的源码可以看出原因:如果是classpath:开头,从classpath加载,否则尝试URL,如果失败,调用 getResourceByPath

public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); } else { try { // Try to parse the location as a URL... URL url = new URL(location); return new UrlResource(url); } catch (MalformedURLException ex) { // No URL -> resolve as resource path. return getResourceByPath(location); } } }

getResourceByPath会被不同 ApplicationContext 实现覆盖.

如 GenericWebApplicationContext覆盖为如下:

protected Resource getResourceByPath(String path) { return new ServletContextResource(this.servletContext, path); } 如 FileSystemXmlApplicationContext覆盖为如下: protected Resource getResourceByPath(String path) { if (path != null && path.startsWith("/")) { path = path.substring(1); } return new FileSystemResource(path); }

最终从文件加载的时候仍然是JAVA中常见的读取文件的方法:

如ClassPathResource得到inputstream的方法是利用class loader.

 public InputStream getInputStream() throws IOException { InputStream is; if (this.clazz != null) { is = this.clazz.getResourceAsStream(this.path); }

如FileSystemResource得到inputstream的方法是利用FileInputStream.

    public InputStream getInputStream() throws IOException {

        return new FileInputStream(this.file);
    }

 public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; }

注意:
用classpath*:需要遍历所有的classpath,所以加载效率会比较差一些,如果只使用classpath:,需要规划好资源文件的路径,尽量避免重名,导致资源文件加载不到的问题。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234042.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • TLSF算法分析[通俗易懂]

    TLSF算法分析[通俗易懂]注:本文的大部分内容摘录自论文《TLSF:aNewDynamicMemoryAllocatorforReal-TimeSystems》,可以通过“科学上网”访问如下链接阅读原文:http://www.gii.upv.es/tlsf/files/ecrts04_tlsf.pdf。什么是TLSFTLSF是TwoLevelSegregatedFitmemoryal

    2022年6月30日
    22
  • idea 2021.5.3 激活码[在线序列号]

    idea 2021.5.3 激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    72
  • Python知识点(史上最全)

    Python知识点(史上最全)Python期末考试知识点(史上最全)python简介Python是一种解释型语言Python使用缩进对齐组织代码执行,所以没有缩进的代码,都会在载入时自动执行数据类型:整形int无限大浮点型float小数复数complex由实数和虚数组成Python中有6个标准的数据类型:

    2022年5月14日
    45
  • idea替换文本快捷键_idea 替换整个项目某个单词

    idea替换文本快捷键_idea 替换整个项目某个单词ctrl+f是查找,ctrl+r是替换1、“ctrl+r”快捷键,用于当前文件内容替换,指的是在当前打开的文件中替换匹配的字符,只操作一个文件;2、“ctrl+shift+r”快捷键,用于在路径中替换。

    2022年9月1日
    7
  • live2d网页看板娘_live2d吧

    live2d网页看板娘_live2d吧Live2D看板娘实现开发工具IDEA国际惯例先上图:所需资源:链接:https://pan.baidu.com/s/1s7IJIqGnn-cNRAfoS-qG5w提取码:dhf4其中包含了看板娘所需的CSS,image,JS,Live2d所需的基本资源。下面就来实现吧开发工具idea+tomcat,如果没有的小伙伴们可以自己从网上分别安装这两个资源并配置,网上有很多,这里我不在赘述了。(如果有很多人需要我或许可以出个教程…认真脸)。1.创建项目2.项目创建完成3.引入看板娘

    2025年8月22日
    38
  • C++ vector的用法(整理)

    C++vector的用法(整理)vector是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器。vector是C++STL的一个重要成员,使用它时需要包含头文件:#include&lt;vector&gt;;一、vector的初始化:可以有五种方式,举例说明如下:(1)vector&lt;int&gt;a(10);//定义了10个整型元素…

    2022年4月4日
    40

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号