简体   繁体   English

Spring classpath前缀差异

[英]Spring classpath prefix difference

Documented here it 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.xml as opposed to classpath:conf/appContext.xml without the asterisk. 使用classpath*:conf/appContext.xml而不是classpath:conf/appContext.xml没有星号有什么区别。

SIMPLE DEFINITION 简单的定义

The classpath*:conf/appContext.xml simply means that all appContext.xml files under conf folders 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.xml will load only one such file ... the first one found on your classpath. 相反, classpath:conf/appContext.xml只会加载一个这样的文件 ......第一个在类路径中找到。

The classpath*:... syntax is useful primarily when you want to build an application context from multiple bean definition files, using wildcard syntax. 当您希望使用通配符语法从多个bean定义文件构建应用程序上下文时, classpath*:...语法非常有用。

For example, if you construct your context using classpath*:appContext.xml , the classpath will be scanned for every resource called appContext.xml in 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.xml will obtain one and only one file called appContext.xml from the the classpath. 相反, classpath:conf/appContext.xml将从classpath:conf/appContext.xml获取一个且只有一个名为appContext.xml文件。 If there is more than one, the others will be ignored. 如果有多个,其他人将被忽略。

classpath*: It refers to a list of resources and loads all such files present in the classpath and list can be empty and if no such file is present in the classpath then application does not throw any exception (just ignores the error). classpath *:它引用资源列表加载类路径和列表中存在的所有此类文件可以为空 ,如果类路径中不 存在此类文件 ,则应用程序不会抛出任何异常 (只是忽略错误)。

classpath: It refers to a certain resource and loads only the first file found on the classpath and if no such file is present in the classpath it will throw an exception classpath:它引用某个资源仅加载在类路径中找到的第一个文件, 如果类路径中没有这样的文件,它将引发异常

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

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)};
      }
   }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM