Spring类路径前缀差异
记录于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单定义
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 underconf
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.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 calledappContext.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 calledappContext.xml
from the the classpath. If there is more than one, the others will be ignored.classpath*:它指的是资源列表并加载类路径中存在的所有此类文件,列表可以是空,如果类路径中不存在这样的文件,那么应用程序不会抛出任何异常(只是忽略错误)。
类路径:它引用特定资源并仅加载在类路径中找到的第一个文件,如果没有此类文件存在于类路径中然后它将抛出异常
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
Spring的源码:
The source code of Spring: