将 Guice 与 OSGi 结合使用
我有一个项目正在尝试转换为 OSGi。然而,这个项目是用 Guice 作为其依赖注入管理器构建的。这是一个大项目,Guice 贯穿始终。 Guice 在转换过程中给了我很多启发。具体来说,它给了我一个 com.google.inject.internal.cglib.reflect.FastClass 的 NoClassDefFoundError 错误。我已确保没有任何内容是包私有的,并且我还下载并使用了 guice-2.0-customloader.jar。尽管如此,还是无济于事。
有走过这条路的人有什么建议吗?值得一提的是,我使用peaberry从OSGi环境(对于插件式架构)获取动态服务,然后在稍后的程序中使用。
多谢, 史蒂夫
I have a project that I am trying to convert to OSGi. However, this project has been built with Guice as its dependency injection manager. It's a large project with Guice interwoven all throughout. Guice has been giving me many fits with the conversion process. Specifically, it has been given me a NoClassDefFoundError for com.google.inject.internal.cglib.reflect.FastClass. I have made sure that nothing is package-private and I also downloaded and used the guice-2.0-customloader.jar. Still, to no avail.
Is there any advice from anyone that has gone down this path? It is worth mentioning that I use peaberry to get dynamic services from the OSGi environment (for a plugin-type architecture) that I then use in the program later on.
Thanks a lot,
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很难确切地说问题是什么,类加载器问题在 OSGi 中可能是非常具体的情况并且难以调试。
如果 guice 确实是您的应用程序的核心,那么将其作为系统捆绑包的片段捆绑包可能是有意义的。系统捆绑包是所有捆绑包的类加载器树中的根节点。如果该包中存在类,则可以在任何地方访问它们。这是我成功地将 Swing LAF(Substance、Alloy、Jide)包装为 OSGi 包的技术。它们必须位于根级别,因为我的所有捆绑包都使用 Swing(尤其是 Swing 类加载器)。
您需要检查 OSGi 规范以创建片段捆绑包并使该片段捆绑包成为框架扩展片段。我只能使用 Eclipse 的 Equinox 运行时来完成此操作。当我编写大部分支持类时,Apache Felix 还不支持框架扩展片段。现在可能已经改变了。我对框架一无所知(knopplefish、spring-dm)
Hard to say exactly what the problem is, classloader issues can be very situation specific in OSGi and hard to debug.
If guice really is a core to your application it may make sense to make it a Fragment bundle of the System Bundle. The System Bundle is the root node in the classloader tree for ALL bundles. If classes exist in this bundle, they'll be accessible everywhere. This is a technique I've used successfully to wrap Swing LAF (Substance, Alloy, Jide) as OSGi bundles. They've gotta be at the root level since all of my bundles use Swing (especially the Swing classloader)
You'll want to check the OSGi specs for creating a Fragment bundle and making that fragment bundle a framework extension fragment. I've only been able to do this using Eclipse's Equinox runtime. At the time I wrote most of the supporting classes, Apache Felix did not yet support framework extension fragments. It may have changed now. I have no idea about frameworks (knopplefish, spring-dm)
只需使用Peaberry即可。它具有在 OSGi 中使用 Guice 所需的所有支持。无需从头开始重新发明一切!
Just use Peaberry. It has all the support you need for using Guice in OSGi. No need to reinvent everything from scratch!
我假设您对 package-private 的评论与 Guice 和桥接类加载器?
有一个错误报告/补丁可能与您的问题相关: http://code.google.com/p/google-guice/issues/detail?id=343
您是否尝试过将 Guice 和 cglib 添加到标准类路径并为它们执行启动委派?这将证明/反驳您的问题与上述错误报告中提到的问题相同。
I'm assuming your comments on package-private are related to this page on Guice and bridging class loaders?
There's a bug report/patch that may be related to your issue: http://code.google.com/p/google-guice/issues/detail?id=343
Have you tried adding Guice and cglib to the standard classpath and doing boot delegation for them? That would prove/disprove your issue is the same one as mentioned on the above bug report.
您可以尝试我针对问题 343 - 刚刚附加了一个新修补的版本,这是最新的 Guice 主干加上这个补丁,所以人们可以尝试它并提供反馈。
最新的补丁避免了许多此类 CGLIB 异常,而无需强制您将类公开,并且还在仍然需要的少数剩余位置(基本上当您显式使用方法拦截时)提供了更好的错误消息。
You could try my latest patch for Issue 343 - just attached a freshly patched build which is the latest Guice trunk plus this one patch, so people can try it and give feedback.
The latest patch avoids a lot of these CGLIB exceptions without forcing you to make classes public, and also provides a better error message in the few remaining places where this is still necessary (basically when you explicitly use method interception).