如何避免 OSGi 中的 class.forName() ?

发布于 2024-11-19 02:13:13 字数 239 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情哥ㄟ 2024-11-26 02:13:13

在 OSGi 中,您将希望远离反射,原因在许多其他地方已经概述。

因此,您的情况是捆绑包 A 需要驻留在捆绑包 B 中的类的某个实例。为了让 A 理解这个实例,我假设它有一些用于与实例通信的接口。让我们把这个说得更具体一点。

/Bundle A
  /ThingyInterface.class

/Bundle B
  /ThingyImplementation.class (implements ThingyInterface.class)

这是一种常规模式:一个包提供接口,另一个包提供实现。现在有两种可能的情况,

  • A 只需要一份实现副本。在这种情况下,请将 Thingy 注册为服务。
  • A 需要多个实现实例。在这种情况下,请在 A 中引入 ThingyFactory,并在 B 中创建该工厂的实现,然后将其注册为服务。

无论哪种情况,您都让 B 进行实际实例化,您没有从 AB 的依赖关系,并且 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 bundle B. For A 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.

/Bundle A
  /ThingyInterface.class

/Bundle B
  /ThingyImplementation.class (implements ThingyInterface.class)

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 the Thingy as a service.
  • A needs several instances of the implementation. In that case, introduce a ThingyFactory in A, and create an implementation of that factory in B, which you then register as a service.

In either case, you let B do the actual instantiation, you have no dependency from A to B, and B doesn't need reflection to instantiate the objects.

In short, services are your friend.

丶情人眼里出诗心の 2024-11-26 02:13:13

在我看来,Java中创建对象的两种方法

  1. 都是通过new调用构造函数
  2. 使用反射

所以如果你想避免反射,你只能导入bundle A中的类,然后new()实例(为了做到这一点,你需要导入B在捆绑包 A) 中。但是由于bundle B已经依赖于A,这将导致OSGi中不允许的交叉依赖。所以我建议你重构你的类以从捆绑包 A 中提取此类代码并移动到捆绑包 B。

In my opinion, two ways to create objects in Java

  1. Call constructer by new
  2. use reflection

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.

暗地喜欢 2024-11-26 02:13:13

我不知道你的确切用例,但听起来你正在制作一些意大利面条。你说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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文