简体   繁体   English

java.util.logging.Logger使用非常特定的ResourceBundle加载行为。 为什么?

[英]java.util.logging.Logger uses very specific ResourceBundle loading behavior. Why?

The java.util.logging.Logger class locates ResourceBundle instances in a very specific manner. java.util.logging.Logger类以非常特定的方式定位ResourceBundle实例。 I am interested in understanding the classloading behavior. 我有兴趣了解类加载行为。

Discarding caching, etc., first the Logger class attempts to use the context classloader (this is good and what I'd expect). 丢弃缓存等,首先Logger类尝试使用上下文类加载器 (这是好的,我期望的)。

If that classloader cannot load a ResourceBundle (using the ResourceBundle#getBundle(String, Locale, ClassLoader) call ), then the system classloader is used next. 如果ResourceBundle#getBundle(String, Locale, ClassLoader)加载器无法加载ResourceBundle (使用ResourceBundle#getBundle(String, Locale, ClassLoader)调用 ),则接下来使用系统类加载器。 That was slightly puzzling to me; 那对我来说有点令人费解; I'd assume that the caller's classloader would be the next one in line. 我假设调用者的类加载器将成为下一个。 Is this an optimization, or is this to handle some specific use case, or...? 这是一个优化,还是处理某些特定用例,或者......?

Finally, if the system classloader cannot load the ResourceBundle , then the inferred call stack (!) is walked, and each class in that stack is mined for its classloader, and each of those is tried in turn. 最后,如果系统类加载器无法加载ResourceBundle ,则推断调用堆栈(!),并且该堆栈中的每个类都为其类加载器挖掘,并且依次尝试每个类。

For all the various classloading scenarios I've encountered or had to write, this one makes the least sense to me. 对于我遇到或必须编写的所有各种类加载场景,这个对我来说没有意义。 I suspect this has to do with Logger 's potential centrality to any given system, but I would welcome any explanation as to why this particular algorithm was selected. 我怀疑这与Logger对任何给定系统的潜在中心性有关,但我欢迎任何关于为什么选择这种特定算法的解释。

(Also, side question not really worthy of its own entry: is there any circumstance in the JVM where the context classloader is null ?) (另外,附带问题并不值得自己输入:JVM中是否存在上下文类加载器null任何情况?)

My guess is they were thinking in a way to support J2EE classloader hierarchies and its different implementations in each application server. 我的猜测是他们正在考虑支持J2EE类加载器层次结构及其在每个应用程序服务器中的不同实现。

Moreover, if we check JavaDoc we have 而且,如果我们检查JavaDoc

As a temporary transition feature in the initial implementation, if the Logger is unable to locate a ResourceBundle from the ContextClassLoader or SystemClassLoader the Logger will also search up the class stack and use successive calling ClassLoaders to try to locate a ResourceBundle. 作为初始实现中的临时转换功能,如果Logger无法从ContextClassLoader或SystemClassLoader中找到ResourceBundle,则Logger还将搜索类堆栈并使用连续调用ClassLoader来尝试查找ResourceBundle。 ( This call stack search is to allow containers to transition to using ContextClassLoaders and is likely to be removed in future versions. ) 此调用堆栈搜索是允许容器转换为使用ContextClassLoaders,并且可能在将来的版本中被删除。

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

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