Spring类路径前缀差异

发布于 2024-09-11 01:12:56 字数 465 浏览 6 评论 0原文

记录于 4.7.2.2 classpath*: 前缀 它指出

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

有人可以解释一下吗?

使用 classpath*:conf/appContext.xml 与使用不带星号的 classpath:conf/appContext.xml 有何区别。

Documented at 4.7.2.2 The classpath*: prefix 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.

Can someone explain this?

What is the difference between using classpath*:conf/appContext.xml as opposed to classpath:conf/appContext.xml without the asterisk.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女中豪杰 2024-09-18 01:12:56

简单定义

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

相反,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.

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

远山浅 2024-09-18 01:12:56

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

例如,如果您使用 classpath*:appContext.xml 构建上下文,则将扫描类路径以查找类路径中名为 appContext.xml 的每个资源以及 bean 定义将它们全部合并到一个上下文中。

相反,classpath:conf/appContext.xml将从类路径中获取一个且唯一一个名为appContext.xml的文件。如果有多个,则其他的将被忽略。

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

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.

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

音盲 2024-09-18 01:12:56

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

类路径:它引用特定资源仅加载在类路径中找到的第一个文件,如果没有此类文件存在于类路径中然后它将抛出异常

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

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: 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 then it will throw an exception

java.io.FileNotFoundException: 
class path resource [conf/appContext.xml] cannot be opened because it does not exist
荒芜了季节 2024-09-18 01:12:56

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

The source code of 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)};
      }
   }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文