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*:的使用是为了多个component(最终发布成不同的jar包)并行开发,各自的bean定义文件按照一定的规则:package+filename,而使用这些component的调用者可以把这些文件都加载进来.

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

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

  1. protected Resource[] findAllClassPathResources(String location) throws IOException {
  2. String path = location;
  3. if (path.startsWith("/")) {
  4. path = path.substring(1);
  5. }
  6. Enumeration resourceUrls = getClassLoader().getResources(path);
  7. Set<Resource> result = new LinkedHashSet<Resource>(16);
  8. while (resourceUrls.hasMoreElements()) {
  9. URL url = (URL) resourceUrls.nextElement();
  10. result.add(convertClassLoaderURL(url));
  11. }
  12. return result.toArray(new Resource[result.size()]);
  13. }

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

另外在加载resource的时候,其他前缀的意义如下表所示:注意classpath*只能用与指定配置文件的路径,不能用在用于
getResource的参数.如
appContext.getResource("classpath*:conf/bfactoryCtx.xml")会异常的.

前缀 例子 说明

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

  1. public Resource getResource(String location) {
  2. Assert.notNull(location, "Location must not be null");
  3. if (location.startsWith(CLASSPATH_URL_PREFIX)) {
  4. return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
  5. }
  6. else {
  7. try {
  8. // Try to parse the location as a URL...
  9. URL url = new URL(location);
  10. return new UrlResource(url);
  11. }
  12. catch (MalformedURLException ex) {
  13. // No URL -> resolve as resource path.
  14. return getResourceByPath(location);
  15. }
  16. }
  17. }

getResourceByPath会被不同ApplicationContext 实现覆盖.

如 GenericWebApplicationContext覆盖为如下:

  1. protected Resource getResourceByPath(String path) {
  2. return new ServletContextResource(this.servletContext, path);
  3. }
  4. 如 FileSystemXmlApplicationContext覆盖为如下:
  5. protected Resource getResourceByPath(String path) {
  6. if (path != null && path.startsWith("/")) {
  7. path = path.substring(1);
  8. }
  9. return new FileSystemResource(path);
  10. }

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

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

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

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

public InputStream getInputStream() throws IOException {
        return new FileInputStream(this.file);
    }

如ServletContextResource得到inputstream的方法是利用servletContext.getResourceAsStream.

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

classpath: spring 中的查找方式的更多相关文章

  1. Spring中基于注解方式管理bean

    操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...

  2. Spring中的注入方式 和使用的注解 详解

    注解:http://www.cnblogs.com/liangxiaofeng/p/6390868.html 注入方式:http://www.cnblogs.com/java-class/p/4727 ...

  3. Java Spring 中你不知道的注入方式

    前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ...

  4. Spring中的注入方式

    在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<propert ...

  5. 170330、Spring中你不知道的注入方式

    前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ...

  6. spring中xml配置方式和注解annoation方式(包括@autowired和@resource)的区别

    xml文件中配置itemSqlParameterSourceProvider是可以的: <bean id="billDbWriter" class="com.aa. ...

  7. 事务理解及Spring中的事务

    一.隔离级别理解 1.脏读 首先理解,一个事务对数据进行了改变,尽管该事务尚未提交,但此时其他事务中的查询语句(注意一定是处于事务中的语句,不处于事务中的语句查到的是正常的)查到的数据,是该事务修改之 ...

  8. spring中获取applicationContext

    常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...

  9. Spring中AOP相关源码解析

    前言 在Spring中AOP是我们使用的非常频繁的一个特性.通过AOP我们可以补足一些面向对象编程中不足或难以实现的部分. AOP 前置理论 首先在学习源码之前我们需要了解关于AOP的相关概念如切点切 ...

随机推荐

  1. linux下载利器之curl和wget的区别

    linux下载利器-------curl和wget的区别 curl和wget基础功能有诸多重叠,如下载等. 在高级用途上的curl由于可自定义各种请求参数所以长于模拟web请求,用于测试网页交互(浏览 ...

  2. pageScope、requestScope、sessionScope、applicationScope的区别

    https://www.cnblogs.com/qianbaidu/p/6006459.html 1.区别: 1.page指当前页面有效.在一个jsp页面里有效 2.request 指在一次请求的全过 ...

  3. Linux基本的指令操作

    绝对路径: 路径的写法,由根目录/写起,例如:/usr/share/doc这个目录. 相对路径: 路径的写法,不由/写起,例如由/usr/share/doc要到/usr/share/man底下时,可以 ...

  4. C语言函数篇(一)函数的组成

    函数的组成: 函数名 输入参数 返回值 返回值 函数名 (输入参数){ 执行体 } 用指针保存函数: int func(int a, int b, char c){ } --> int (*fu ...

  5. 匿名函数lambda python

    lambda 的主体是一个表达式,不是一个代码块lambda 只有一行,仅仅能在lambda表达式种封装有限的逻辑进去匿名函数:需要一个函数,而又不想动脑筋去想名字 #普通函数的定义 def f(a, ...

  6. 6 Django的视图层

    视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. . ...

  7. SharpCompress的压缩文件解压和文件夹压缩

    1.前言 最近做一个功能需要用到对压缩文件的解压,就找到了这个SharpCompress不错,还能解压rar的文件.但是网上的资料和我拿到的SharpCompress.dll的方法有些出入,所以我就自 ...

  8. 洛谷P1605 迷宫

    迷宫 题目链接 这道题就是一道简单的dfs计方案数qwq. 我的思路是把表初始化为1,再将障碍改为0,因为在全局定义中数组会直接初始化为0,所以就少去了对边界的特判. next数组加循环可以减少代码量 ...

  9. long转int

    由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参考: 一.强制类型转换 [java] l ...

  10. 【Substring with Concatenation of All Words】cpp

    题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...