Java Spring 类路径前缀区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3294423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:49:28  来源:igfitidea点击:

Spring classpath prefix difference

javaspringclasspath

提问by JavaRocky

Documented hereit states

记录在这里它指出

This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(...) call), and then merged to form the final application context definition.

这个特殊的前缀指定必须获取与给定名称匹配的所有类路径资源(在内部,这基本上是通过 ClassLoader.getResources(...) 调用发生的),然后合并以形成最终的应用程序上下文定义。

Can someone explain this?

有人可以解释一下吗?

What is the difference between using classpath*:conf/appContext.xmlas opposed to classpath:conf/appContext.xmlwithout the asterisk.

使用classpath*:conf/appContext.xmlclasspath:conf/appContext.xml不使用星号有什么区别。

采纳答案by Eugene Ryzhikov

SIMPLE DEFINITION

简单定义

The classpath*:conf/appContext.xmlsimply means that all appContext.xml filesunder conffolders in all your jars on the classpath will be picked up and joined into one big application context.

classpath*:conf/appContext.xml只是意味着类路径上所有 jar文件conf夹下的所有 appContext.xml 文件都将被拾取并加入一个大的应用程序上下文中。

In contrast, classpath:conf/appContext.xmlwill load only one such file... the first one found on your classpath.

相反,classpath:conf/appContext.xml加载一个这样的文件......在您的类路径中找到的第一个文件

回答by skaffman

The classpath*:...syntax is useful primarily when you want to build an application context from multiple bean definition files, using wildcard syntax.

classpath*:...当您想使用通配符语法从多个 bean 定义文件构建应用程序上下文时,该语法主要有用。

For example, if you construct your context using classpath*:appContext.xml, the classpath will be scanned for every resource called appContext.xmlin the classpath, and the bean definitions from all of them merged into a single context.

例如,如果您使用 构建上下文classpath*:appContext.xml,类路径中将扫描类路径中调用的每个资源appContext.xml,并将所有这些资源的 bean 定义合并到单个上下文中。

In contrast, classpath:conf/appContext.xmlwill obtain one and only one file called appContext.xmlfrom the the classpath. If there is more than one, the others will be ignored.

相比之下,classpath:conf/appContext.xmlappContext.xml从类路径中获取一个且只有一个调用的文件。如果有多个,其他将被忽略。

回答by Bacteria

classpath*:It refers to a list of resourcesand loads allsuch files present in the classpath and list can be emptyand if no such file is presentin the classpath then application does not throw any exception(just ignores the error).

类路径*:它指的是资源列表加载类路径中存在的所有此类文件,列表可以为空,如果类路径中不存在此类文件,则应用程序不会引发任何异常(只是忽略错误)。

classpath:It refers to a certain resourceand loads only the firstfile found on the classpath and if no such file is present in the classpath it will throw an exception

类路径:它指的是某个资源并且只加载在类路径中找到的第一个文件,如果类路径中不存在这样的文件,它将抛出异常

java.io.FileNotFoundException: class path resource [conf/appContext.xml] cannot be opened because it does not exist

回答by zzz

The source code of Spring:

Spring的源代码:

public Resource[] getResources(String locationPattern) throws IOException {
   Assert.notNull(locationPattern, "Location pattern must not be null");
   //CLASSPATH_ALL_URL_PREFIX="classpath*:"
   if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
      // a class path resource (multiple resources for same name possible)
      if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
         // a class path resource pattern
         return findPathMatchingResources(locationPattern);
      }
      else {
         // all class path resources with the given name
         return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
      }
   }
   else {
      // Only look for a pattern after a prefix here
      // (to not get fooled by a pattern symbol in a strange prefix).
      int prefixEnd = locationPattern.indexOf(":") + 1;
      if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
         // a file pattern
         return findPathMatchingResources(locationPattern);
      }
      else {
         // a single resource with the given name
         return new Resource[] {getResourceLoader().getResource(locationPattern)};
      }
   }
}