java 了解 Thread.currentThread().getContextClassLoader().getResourceAsStream()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12645298/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:40:55  来源:igfitidea点击:

Understanding Thread.currentThread().getContextClassLoader().getResourceAsStream()

javaclassloadercontextclassloader

提问by Tony

I am looking at a code example, and I am not sure what this means.

我正在查看代码示例,但不确定这意味着什么。

Thread.currentThread().getContextClassLoader()
    .getResourceAsStream("MyProperty.properties");

It appears that it looking to read a property file but I am not sure where MyProperty.properties is located.

看起来它想读取一个属性文件,但我不确定 MyProperty.properties 所在的位置。

I appreciate for any help, thanks.

我感谢任何帮助,谢谢。

采纳答案by Gray

It appears that it looking to read a property file but I am not sure where MyProperty.propertiesis located.

看起来它想读取一个属性文件,但我不确定它在哪里MyProperty.properties

As you currently have it there, that will look for the MyProperty.propertiesfile at the top of your classpath. The could be in your src/main/resourcesdirectory or other src folder -- it would depend on how your application (jar/war) is built.

正如您目前拥有的那样,它将MyProperty.properties在您的类路径顶部查找文件。可能在您的src/main/resources目录或其他 src 文件夹中——这取决于您的应用程序 (jar/war) 的构建方式。

If you are building a jar then you should be able to unpack it and see your properties file at the top level of the jar. If you are building a war, maybe it should be in the WEB-INF/classesdirectory. Again, it depends on how it was built.

如果您正在构建一个 jar,那么您应该能够解压缩它并在 jar 的顶层查看您的属性文件。如果您正在构建War,也许它应该在WEB-INF/classes目录中。同样,这取决于它是如何构建的。

Typically the log4j.propertiesfile is also at the top of the classpath so you may want to look for that file and put your property file alongside it.

通常该log4j.properties文件也位于类路径的顶部,因此您可能需要查找该文件并将您的属性文件放在它旁边。