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
Spring classpath prefix difference
提问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.xml
as opposed to classpath:conf/appContext.xml
without the asterisk.
使用classpath*:conf/appContext.xml
与classpath:conf/appContext.xml
不使用星号有什么区别。
采纳答案by Eugene Ryzhikov
SIMPLE DEFINITION
简单定义
The classpath*:conf/appContext.xml
simply means that all appContext.xml filesunder 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
将只加载一个这样的文件......在您的类路径中找到的第一个文件。
回答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.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. If there is more than one, the others will be ignored.
相比之下,classpath:conf/appContext.xml
会appContext.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)};
}
}
}