java - 类加载器 : Resources from Callee

标签 java gradle

我有一个包含两个子项目的项目。

parent
 |
 + - - my-api
 |     |
 |      ` - config.properties
 |
 ` - - my-project
       |
        ` - config.properties

my-api 包含使用加载资源的方法

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream input = loader.getResourceAsStream(name);

my-project调用my-api的方法来加载存储在my-project/src/test/resources中的config.properties

当我删除 my-api/src/test/resources/config.properties 时,my-project 中的 config.properties 会正确加载。但是,如果我不删除它,则会加载 my-api/src/test/resources/config.properties

最佳答案

您所看到的行为绝非意外。

在正常的 Java 设置中,几乎所有代码(除了核心低级库)都共享同一个 ClassLoader,它使用 CLASSPATH 查找其资源。如果多个类路径条目包含具有相同路径的资源,则将返回属于类路径中第一个出现的条目的资源。来自 URLClassLoader documentation :

The URLs will be searched in the order specified for classes and resources after first searching in the specified parent class loader.

在您的情况下,my-api 必须首先出现在类路径上,因此只要它包含 config.properties 资源,该资源就会优先于包含在 my-project 中。

您似乎想要加载与特定调用类驻留在同一类路径条目中的config.properties。这是可能的,但它需要您手动确定调用类所在的类路径条目的根。它涉及相当多的代码,并且很难涵盖所有可能的类加载情况(即类文件夹、jar、下游项目构建的阴影 jar、通过网络加载的类、下游使用的自定义类加载器、安全管理器等)。因此,我不会在这里发布如何执行此操作的详细信息,因为我认为这是一个坏主意。如果您确实想走这条路,请首先查看:

Foo.class.getProtectionDomain().getCodeSource().getLocation()

那么,有哪些替代方案?

您有多种选择,但我的偏好只是将各种 config.properties 文件放在不同的包中。为每个项目使用不同的基础包通常是一个很好的做法,因此您可以执行以下操作:

parent
 |
 + - - my-api
 |     |
 |      ` - - com.my.api
 |            |
 |             ` - config.properties
 |
 ` - - my-project
       |
        ` - - com.my.project
              |
               ` - config.properties

然后,您有两种选择如何加载这些文件。如果您知道某个类与每个属性文件位于同一包中,则可以使用该类加载它:

void loadConfig(Class<?> sibling) {
    InputStream config = sibling.getResourceAsStream("config.properties");
    //...
}

或者,您可以简单地传入包名称:

void loadConfig(String packageName) {
    String path = "/" + packageName.replace(".", "/") + "/config.properties";
    InputStream config = this.getClass().getClassLoader().getResourceAsStream(path);
    //...
}

如果您确实想要确定被调用者的包,您可以使用第一种方法和一个丑陋的黑客:

void loadConfig() {
    try {
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        Class<?> sibling = Class.forName(stackTrace[1].getClassName());
        InputStream config = sibling.getResourceAsStream("config.properties");
        //...
    } catch (ClassNotFoundException e) {
        // Calling class must have been loaded using a different ClassLoader.
        // There's might be some complex code that could be used to recover here,
        // but this should happen rarely, so maybe just propogate Exception
    }
}

我不推荐最后一种方法。

关于java - 类加载器 : Resources from Callee,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58133494/

相关文章:

java - 使用 SonarQube Scanner 分析 java 代码时出错

Gradle应用程序插件: Forcing Script to include lib/* in Classpath

java - Java 中 HashMap 的用处

java - 任务 ':compileTestJava'的Gradle执行失败

Java SSL - 以编程方式将自签名证书添加到 cacerts

java - Vaadin 组件的 Selenium WebDriver 自动化

maven - 我可以在 Gradle 中使用 Maven 插件(发音)吗?

gradle - Windows上的Gradle执行命令

java - 将 VLCJ 媒体播放器添加到 JavaFX 中的 Canvas

java - 使用客户端上的正确程序打开 webdav 链接