如何避免 OSGi 中的 class.forName() ?
我对 OSGi 和我们部门还比较陌生。正在转向 OSGi 框架。我有两个包 A 和 B。B 依赖于 A,因此我将其作为 Import-Package:A 包含在 B 的清单文件中。
另外,我在 A 中有一个类,它使用反射来访问 B 中的某个类。 A 中的类使用 class.forName(B 中的类)。我想摆脱这种反射,因为当我转向 OSGi 框架时这可能会导致问题。我怎样才能摆脱这个class.forName()?
谢谢!!
I am relatively new to OSGi and our dept. is shifting to OSGi framework. I have two bundles A and B. B depends on A so I have included it in B's manifest file as Import-Package:A.
Also, I have a certain class in A which uses reflection to access a certain class from B.
Class in A uses class.forName(class in B). I want to get rid of this reflection as this might cause problems when I shift to OSGi framework. How can I get rid of this class.forName()?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 OSGi 中,您将希望远离反射,原因在许多其他地方已经概述。
因此,您的情况是捆绑包
A
需要驻留在捆绑包B
中的类的某个实例。为了让A
理解这个实例,我假设它有一些用于与实例通信的接口。让我们把这个说得更具体一点。这是一种常规模式:一个包提供接口,另一个包提供实现。现在有两种可能的情况,
A
只需要一份实现副本。在这种情况下,请将Thingy
注册为服务。A
需要多个实现实例。在这种情况下,请在A
中引入ThingyFactory
,并在B
中创建该工厂的实现,然后将其注册为服务。无论哪种情况,您都让
B
进行实际实例化,您没有从A
到B
的依赖关系,并且B
> 不需要反射来实例化对象。简而言之,服务是您的朋友。
In OSGi you will want to stay away from reflection, for reasons outlined in a lot of other places.
So, your situation is that bundle
A
needs some instance of a class which resides in bundleB
. ForA
to make sense of this instance, I will assume it has some interface that it will use to talk to the instance. Let's make this a little more concrete.This is a regular pattern: one bundle provides an interface, the other provides the implemenatation. There are now two possible situations,
A
needs exactly one copy of the implementation. In that case, register theThingy
as a service.A
needs several instances of the implementation. In that case, introduce aThingyFactory
inA
, and create an implementation of that factory inB
, which you then register as a service.In either case, you let
B
do the actual instantiation, you have no dependency fromA
toB
, andB
doesn't need reflection to instantiate the objects.In short, services are your friend.
在我看来,Java中创建对象的两种方法
所以如果你想避免反射,你只能导入bundle A中的类,然后new()实例(为了做到这一点,你需要导入B在捆绑包 A) 中。但是由于bundle B已经依赖于A,这将导致OSGi中不允许的交叉依赖。所以我建议你重构你的类以从捆绑包 A 中提取此类代码并移动到捆绑包 B。
In my opinion, two ways to create objects in Java
So if u want to avoid reflection, you can only import class in bundle A,then new() instance(In order to do this, u need imprt B in bundle A). But as bundle B alreadys depends on A, this will cause cross-dependence which isn't allowed in OSGi. So I suggest u refactor you class to extract such code from bundle A and move to bundle B.
我不知道你的确切用例,但听起来你正在制作一些意大利面条。你说Bundle B依赖于A,但A必须通过反射从B加载一个类。这意味着 A 也依赖于 B。既然你不能有 B -> A-> B、需要删除其中一个依赖项。
或者您可能需要另一个包 C,其中包含 A 和 B 所共有的任何类。请记住,OSGi 可以帮助您创建具有干净接口的模块。如果您发现在使用 OSGi 进行某些操作时遇到困难,可能是因为某些抽象正在泄漏或某些依赖关系尚未设计良好。
I don't know your exact use case, but it sounds like you have some spaghetti in the making. You said that Bundle B depends on A, and yet A has to load a class from B via reflection. This means that A depends on B as well. Since you cannot have B -> A -> B, you need to remove one of the dependencies.
Or perhaps you need another bundle C, which contains whatever classes are common to both A and B. Remember that OSGi is there to help you create modules with clean interfaces. If you find you are having trouble making something work with OSGi, it is probably because some abstraction is leaking or some dependency has not been well designed.