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. SPOJ1026 概率DP

    Favorite Dice BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorit ...

  2. POJ:3104-Drying(神奇的二分)

    Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20586 Accepted: 5186 Description I ...

  3. [洛谷1156]垃圾陷阱(DP)

    [Luogu1156] f[i]表示高度为i时的存活时间 Code #include <cstdio> #include <algorithm> #define N 110 u ...

  4. python基础之装饰器扩展和迭代器

    wraps模块 让原函数保留原来的说明信息 import time import random from functools import wraps def auth(func): '''auth ...

  5. 20145202马超 《Java程序设计》第七周学习总结

    Arrays:用于操作数组的工具类. 里面都是静态方法. asList:将数组变成list集合. 把数组变成集合的好处:可以使用集合的思想来操作数组中的元素. 将数组变成集合的时候不可以使用集合的增删 ...

  6. P1498 南蛮图腾

    P1498 南蛮图腾 题目描述 自从到了南蛮之地,孔明不仅把孟获收拾的服服帖帖,而且还发现了不少少数民族的智慧,他发现少数民族的图腾往往有着一种分形的效果(看Hint),在得到了酋长的传授后,孔明掌握 ...

  7. dex2oat 加载多次

    我是一个做插件开发的人员,插件就是动态加载dex文件. 然后4.4以后,Android dalvik虚拟机,和ART. art运行的必须要把dex转换为oat elf文件. 然后,这个dex2oat需 ...

  8. Java 泛型 二

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  9. MySQL添加和删除字段

    查询表的字段类型: mysql> desc t_template_title; +----------------+--------------+------+-----+---------+- ...

  10. 《1024伐木累》-BUG的通用解决办法

    本周月侠出场,一番侠骨柔情,或许你会为丽姐担忧,或许你也很想知道,发现了一个不该发现的秘密,月侠的未来究竟会怎样,但是一切都只是一个开头,伴随故事成长,伴随故事了解时事,尤其是IT圈子里的事儿,或许真 ...