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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ip地址的组成(网络位+主机位)

    ip地址的组成(网络位+主机位)IP地址采用分层结构;IP地址是由网络号(netID)与主机号(hostID)两部分组成的。根据不同的取值范围,IP地址可以分为五类;IP地址中的前5位用于标识IP地址的类别:实际大多通过子

    2022年8月5日
    10
  • Vue Router Tab「建议收藏」

    Vue Router Tab「建议收藏」介绍VueRouterTab是基于Vue.js和VueRouter的路由页签组件,用来实现多页签页面的管理。官网演示包含的功能✅响应路由变化来打开或切换页签✅页签过多鼠标滚轮滚动✅页签拖拽排序✅支持页签打开、切换、关闭、刷新、重置等操作✅Iframe页签嵌入外部网站✅组件个性化设置:过渡效果、自定义插槽、页签右键菜单✅多语言支持✅缓存控制:页签规则、页签是否缓存、最大缓存数、是否复用组件等✅动态页签信息:标题、图标、提示✅初始页签数据,进入页

    2022年7月27日
    14
  • 讨论JDK的File.equal()

    讨论JDK的File.equal()

    2022年1月17日
    40
  • 主导家电渠道商洗牌的另有其人

    主导家电渠道商洗牌的另有其人

    2022年3月5日
    29
  • centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码

    centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码本文介绍了Centos7修改mysql指定用户的密码,具体如下:1.登陆mysql或者mariadb(两种任选其一)[root@localhost~]#mysql-uroot[root@localhost~]#mysql-uroot-p2.切换到存储用户名和密码的数据库MariaDB[mysql]>usemysql;回车,会显示以下内容Readingtablein…

    2022年6月21日
    27
  • 2021年前端面试题及答案

    前端面试汇总(2020年)一大纲1、前言2、前端工程化3、前端设计模式4、前端安全性问题5、前端跨域问题6、前端数据加密7、前端http相关问题8、*前端基础知识点面试题9、前端技术栈问题前言由于新冠肺炎疫情,现在成天呆在家里,加上也要准备面试,就在家里看面试题…

    2022年4月6日
    53

发表回复

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

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