如何在 Equinox 中获取捆绑包的类加载器?
我已经为此阅读了很多 Equinox 代码,但仍然无法找出一种在 Eclipse Equinox 设置中获取 osgi 包的类加载器的非 hacky 方法。有吗?
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. Is there one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 OSGi 4.3 中您可以使用:
In OSGi 4.3 you can use:
简短的回答(对于 OSGi 4.1 来说肯定如此,4.2 不确定)是你无法获得包的类加载器。但是
Bundle
接口公开了loadClass()
方法,这将允许您编写一个类加载器来包装捆绑 API 并委托给该loadClass()
方法。或者您可以节省一些时间并使用 Spring DM 的BundleDelegatingClassLoader
类代替。The short answer (certainly for OSGi 4.1, not sure of 4.2) is you can't get a bundle's classloader. However the
Bundle
interface exposes aloadClass()
method and this would allow you to write a classloader that wraps the bundle API and delegates to thatloadClass()
method. Or you can save some time and use Spring DM'sBundleDelegatingClassLoader
class instead.Bundle的类加载器可以通过BundleWiring接口获取。这是一个简短的例子:
The class loader of a bundle can be obtained through the BundleWiring interface. Here a short example:
在普通的 java 代码中,您可以使用 来获取加载给定对象的类加载器,
或者甚至
同样适用于 Equinox,只需使用来自您感兴趣的捆绑包的对象或类型。
In normal java code, you can get the class loader that loaded a given object with
Or even just
The same applies to Equinox, just use an object or type that comes from the bundle you are interested in.