简体   繁体   English

类加载器如何找到资源?

[英]How does the classloader find resources?

I am reviewing a small github example project ( shagie/TestingWithHsqldb ) and have stumbled across a convention that is new to me and was hoping someone could help me understand what I am looking at. 我正在审查一个小型github示例项目( shagie / TestingWithHsqldb ),偶然发现了一个对我来说是新的约定,希望有人可以帮助我了解我在看什么。


The project is organized under the main src directory as such... 该项目在src主目录下组织,例如...

src/main/resources/connection_config.properties
src/main/java/com/shagie/dbtest/db/DBConnection.java
src/main/java/com/shagie/dbtest/db/DataAccess.java

src/test/resources/connection_config.properties
src/test/java/com/shagie/dbtest/db/DataAccessTest.java

The code in DBConnection.java is called both from DataAccess.java under the 'main' directory as well as the DataAccessTest.java in the 'test' directory. DBConnection.java的代码既可以从“主”目录下的DataAccess.java调用,也可以从“测试”目录中的DataAccessTest.java中调用。

In the file DBConnection.java there is the following statement that imports the connection_config.properties file: 在文件DBConnection.java有以下语句可导入connection_config.properties文件:

Properties prop = new Properties();
InputStream in = GetClass().getResourceAsStream("/connection_config.properties");
prop.load(in);
in.close();

My Questions... 我的问题...

  • How is the properties file being found in the "resources" directory if the call is structured as getResourceAsStream("/connection_config.properties") ? 如果调用的结构为getResourceAsStream("/connection_config.properties")如何在“资源”目录中找到属性文件? Doesn't that path mean it should look at the root directory (main or test) for the properties file? 该路径是否意味着它应该查看属性文件的根目录(主目录或测试目录)?

  • Since DBConnection.java doesn't change it's root 'main' directory, how is it that the properties file comes from the 'test' directory when executing DataAccessTest.java 由于DBConnection.java不会更改其根目录“ main”,因此执行DataAccessTest.java时属性文件如何来自“ test”目录

  • I assume this pattern is related to dependency injection and unit testing. 我认为这种模式与依赖注入和单元测试有关。 Is there a name for this specific pattern? 此特定模式有名称吗? Where is a good place to learn more about it? 在哪里可以了解更多信息?

EDIT: Adjusting question to focus on the ClassLoader 's getResource functionality instead of dependency injection 编辑:调整问题以专注于ClassLoadergetResource功能,而不是依赖项注入

This has nothing to do with dependency injection but with how ClassLoader s resolve resource paths. 这与依赖注入无关,但与ClassLoader解析资源路径的方式有关。

1) This might be a bit confusing coming from a Linux background indeed getResourceAsStream(resource) has different rules. 1)这可能有点令人困惑,因为Linux背景确实使getResourceAsStream(resource)具有不同的规则。 As per the doc : 根据文档

If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'. 如果名称以'/'('\\ u002f')开头,则资源的绝对名称是名称中'/'之后的部分。

So the leading slash here only tells the class loader how to get the absolute name (whether the name you passed should be prepended with the package name or not), not that it should be looking "at the root" (in the test/main folders). 因此,这里的前导斜线仅告诉类加载器如何获取绝对名称(通过的名称是否应该以软件包名称开头),而不是它应该在“根目录”中查找(在test / main中)文件夹)。 What is the root and how resolution works depends on the class loader you are using. 根源是什么,解析的工作方式取决于您使用的类加载器。 By default (in this case) resources are searched in the resources folder. 默认情况下(在这种情况下)在resources文件夹中搜索resources You can write your own ClassLoader and change that behavior. 您可以编写自己的ClassLoader并更改该行为。

2) Again, when calling getResources() or getResourceAsStream() the class delegates it to the ClassLoader which loaded this class. 2)同样,当调用getResources()getResourceAsStream() ,类将其委托给ClassLoader了该类的ClassLoader If you are running unit tests (Junit or something similar) then the ClassLoader will know that it's supposed to look for resources in test folder not main . 如果您正在运行单元测试(Junit或类似的东西),那么ClassLoader会知道它应该在test文件夹(不是main寻找资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM