ClassLoader.getSystemClassLoader()与obj.getClass()。getClassLoader()。getSystemClassLoader() [英] ClassLoader.getSystemClassLoader() vs obj.getClass().getClassLoader().getSystemClassLoader()

查看:165
本文介绍了ClassLoader.getSystemClassLoader()与obj.getClass()。getClassLoader()。getSystemClassLoader()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是否完全相同:

ClassLoader.getSystemClassLoader() // 1

vs:

obj.getClass().getClassLoader().getSystemClassLoader() // 2
Person.class.getClassLoader().getSystemClassLoader()

是否存在可能产生不同结果的情况?

推荐答案

每个 ClassLoader .getSystemClassLoader() javadoc 通常是用于启动应用程序的类加载器。 java.system.class.loader 属性可用于覆盖返回的类加载器,但是:

As per ClassLoader.getSystemClassLoader() javadoc this is normally the class loader used to start the application. The java.system.class.loader property can be used to override the returned class loader, however:


在虚拟机几乎完全初始化之前,不会检查覆盖系统类加载器的系统属性。在启动过程中执行此方法的代码应注意,在系统完全初始化之前,不要缓存返回值。

The system property to override the system class loader is not examined until the VM is almost fully initialized. Code that executes this method during startup should take care not to cache the return value until the system is fully initialized.

在更复杂的设置中 obj.getClass()。getClassLoader() Person.class.getClassLoader()可以返回自定义类加载器,例如 OSGI 。这个自定义类加载器完全取决于返回系统类加载器。
可能选择不使用因为它会绕过OSGI边界类的加载边界,请参见此答案

In more complex setups obj.getClass().getClassLoader() or Person.class.getClassLoader() can return a custom class loader e.g. OSGI. It's entirely up to this custom class loader to return the system class loader. It might choose not e.g. because it would bypass the OSGI boundle class loading boundaries, see this answer.

因此大多数情况下它们应该是相同的,但是没有什么可以阻止您配置JVM或编写使它们与众不同的软件。

So most of the time they should be the same, but nothing stops you from configuring the JVM or writing software that would make them different.

这篇关于ClassLoader.getSystemClassLoader()与obj.getClass()。getClassLoader()。getSystemClassLoader()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆