[英]How to get classloader for a bundle in equinox?
I have read a lot of equinox code for this, but still can't figure out a non-hacky way of getting the classloader for a osgi bundle in eclipse equinox setup. 我已经为此阅读了很多equinox代码,但仍然无法弄清楚在eclipse equinox设置中获取osgi包的类加载器的非hacky方法。 Is there one? 有吗?
在OSGi 4.3中,您可以使用:
bundle.adapt(BundleWiring.class).getClassLoader()
The short answer (certainly for OSGi 4.1, not sure of 4.2) is you can't get a bundle's classloader. 简短的回答(当然对于OSGi 4.1,不确定4.2)是你无法得到一个bundle的类加载器。 However the Bundle
interface exposes a loadClass()
method and this would allow you to write a classloader that wraps the bundle API and delegates to that loadClass()
method. 但是Bundle
接口公开了一个loadClass()
方法,这将允许您编写一个包装bundle API并委托给该loadClass()
方法的类加载器。 Or you can save some time and use Spring DM's BundleDelegatingClassLoader
class instead. 或者你可以节省一些时间并使用Spring DM的BundleDelegatingClassLoader
类。
The class loader of a bundle can be obtained through the BundleWiring interface. 可以通过BundleWiring接口获取bundle的类加载器。 Here a short example: 这是一个简短的例子:
Bundle bundle = bundleContext.getBundle();
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
ClassLoader classLoader = bundleWiring.getClassLoader();
In normal java code, you can get the class loader that loaded a given object with 在普通的java代码中,您可以获取加载给定对象的类加载器
object.getClass().getClassLoader();
Or even just 甚至只是
SomeType.class.getClassLoader();
The same applies to Equinox, just use an object or type that comes from the bundle you are interested in. 这同样适用于Equinox,只需使用来自您感兴趣的包的对象或类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.