gpt4 book ai didi

java - 类加载器和服务加载器。使用哪个类加载器?

转载 作者:行者123 更新时间:2023-12-02 02:25:43 24 4
gpt4 key购买 nike

我正在编写一个可扩展的应用程序。

阅读有关 ServiceLoader 的文档实用程序类,我无法理解以下句子:

The provider must be accessible from the same class loader that was initially queried to locate the configuration file; note that this is not necessarily the class loader from which the file was actually loaded.

谁能帮我解释一下这是什么意思吗?

如何确定使用哪个类加载器来定位配置文件?

最佳答案

Could anyone explain to me what does it mean?

我认为它指的是ClassLoaders这一事实可以并且确实执行委派。如果我表演,比如说,

MyClass.class.getClassLoader().getResource("/services/org/my/Service");

找到配置文件,那么可能就是ClassLoader对于 MyClass委托(delegate)给家长 ClassLoader ,也许还有它的父级,等等,这样它就不是 ClassLoader我查询( MyClass.class.getClassLoader() )实际加载了配置文件。

在这种情况下,规范表示无论哪个 ClassLoader 实际上找到配置文件,它命名的服务类必须可以从MyClass.class.getClassLoader()访问。我在这里使用“可访问”并不是为了排除它自己的代表团。

在实践中,这些规定对于 ClassLoader 可能更为引人注目。遵循比简单委托(delegate)类加载器链更有趣的策略的实现,例如在 JavaEE 容器中可以找到。

How could I determine which class loader it is used to locate the configuration file?

一般来说,您可能不需要这样做。我建议最好的策略是确保任何给定服务配置文件命名的服务类都可以通过(全部)相同的 ClassLoader 进行访问。 s 作为配置文件本身。将它们放在同一个jar中例如,应该实现这一点。

关于java - 类加载器和服务加载器。使用哪个类加载器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47802587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com