我们希望将.properties
文件保留在jar之外,这样就可以更改模块中使用的属性,而不必重新安装模块。
在Java 8之前,我们通常运行一个脚本,并以下面的方式包含.properties
文件,并且它起了作用。但是当我们更新到Java8时,这种将.properties
文件包含在类路径中的方法无法工作,这意味着Java程序无法找到.properties
文件。
脚本看起来像:
/usr/java/latest/bin/java -d64 -Xms1G -Xmx16G -XX:MaxPermSize=128m -XX:-UseGCOverheadLimit -cp "/online/sand/lib/client-api-1.0.0.jar:/online/sand/oap_pub/lib/*:/online/sand/oap/oap_dw/run/client_api/application.properties" team.online.client.api.MasterProcessor | tee -a client_api.log
我们使用Sping上下文以这样的方式获取属性文件:
<util:properties id="app_props"
location="classpath*:/application.properties" />
然后以这种方式使用appilcation.properties文件中的一个属性(在许多不同的文件中):
@Value( "#{app_props[\"SERVICE_PATH_GET_METADATA\"]?:''}" )
private String metadataServicePath;
的问题是:
Spring在"classpath*:/“中只看到了jar文件吗?
就像我使用下面的代码查看Spring的类路径中的内容一样:
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(new DefaultResourceLoader());
Resource[] resources = new Resource[0];
try {
resources = patternResolver.getResources("classpath*:*");
} catch (IOException e) {
e.printStackTrace();
}
for (Resource resource : resources) {
System.out.println("spring context classpath : " +resource.getDescription());
}
我是,不是,我在类路径上看到了application.properties
。看起来,jars中的文件在类路径中对Spring是可见的。
但是如果我像这样使用ClassLoader打印:
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for (URL url: urls) {
System.out.println("classloader classpath : " + url.getFile());
}
我在类路径上看到了这一点:
/online/sand/oap/oap_dw/run/client_api/application.properties
对这两种指纹的区别有什么见解吗?
如何将这个application.properties
文件包含在Spring类路径中?
发布于 2018-03-31 11:26:56
若要查找属性文件,只需指定没有属性文件名的目录。java需要类路径上的jars或目录,而不是文件。
试试. -cp -cp team.online.client.api.MasterProcessor ..。
顺便说一句:如果字符串的一部分被双引号包围,星号就不会展开。如果jar文件名中没有空白,只需去掉双引号即可。
https://stackoverflow.com/questions/45467291
复制相似问题