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. html中table,tr,td

    table表格,tr表格中的行,tr表格中的列,等级关系是table>tr>td, 当然表格中还包括thead,tbody,tfoot,th,但由于浏览器支持缘故很少使用.另外table在 ...

  2. JZOJ 5913. 林下风气

    Description 里口福因有林下风气,带领全国各地高校掀起了一股AK风,大家都十分痴迷于AK.里口福为了打击大家的自信心,出了一道自以为十分困难的题目.里口福有一棵树,第i个节点上有点权ai,他 ...

  3. 牛客暑假多校第二场J-farm

    一.题意 White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. T ...

  4. 笔记-HTTP及HTTPS

    笔记-HTTP及HTTPS 1.      HTTP 1.1.    简介 HTTP:hyper text transfer protocol 超文本传输协议 http常用请求方式,method ge ...

  5. 我理解中的Hadoop HDFS分布式文件系统

    一,什么是分布式文件系统,分布式文件系统能干什么 在学习一个文件系统时,首先我先想到的是,学习它能为我们提供什么样的服务,它的价值在哪里,为什么要去学它.以这样的方式去理解它之后在日后的深入学习中才能 ...

  6. ListView Viewholder的坑 线性布局的坑

    1.ListView Viewholder的坑 /** * 默认带图片的menu adapter */ public static class MenuImageAdapter extends Bas ...

  7. pdo事务

    $pdo->beginTransaction() $pdo->commit() $pdo->rollback();

  8. ElasticSearch学习笔记(二)-- mapping

    1. mapping简介 2. 自定义 mapping 3. mapping演示 创建索引,设置mapping,可以新增字段 get一下mapping 设置索引的字段不可新增 为索引添加字段,发现报错 ...

  9. laravel5.5契约

    无规矩不成方圆, Laravel 的契约是一组定义框架提供的核心服务的接口,规定了实现该接口的规范. 为什么要使用接口 首先,让我们来看一些高耦合缓存实现的代码.如下: <?php namesp ...

  10. Erlang中atom的实现[转]

    转自: http://www.cnblogs.com/zhengsyao/p/3424539.html 在 Erlang 中,使用 atom 既方便又高效,我们就来看看 atom 是怎么实现的.ato ...