创建 Java 插件

发布于 2025-01-16 18:47:47 字数 556 浏览 2 评论 0原文

我有两个 java 包 - package-A、package-B 假设这是包的依赖链。

package-A -> package-B

我想传递将在 package-A 中执行的自定义 Java 函数。我该怎么做?

package-A 
   - src
       - Foo.java

package-B
   - src
       - Bar.java
       - Tag.java
class Foo implements Tag {
  doThis() {
       // do what I say.
  }
}

------------------------------------------
class Bar {
    execute() {
        tag.doThis();
    }
}

interface Tag {
  doThis();
}

我想要某种插件框架,以便包 A 所有者可以向库包 B 的所有用户提供实现,而包 B 不必依赖于包 A

I have two java packages - package-A, package-B
Let us say this is the dependency chains of the packages.

package-A -> package-B

I want to pass custom Java function that would be executed in package-A. How do I do that?

package-A 
   - src
       - Foo.java

package-B
   - src
       - Bar.java
       - Tag.java
class Foo implements Tag {
  doThis() {
       // do what I say.
  }
}

------------------------------------------
class Bar {
    execute() {
        tag.doThis();
    }
}

interface Tag {
  doThis();
}

I want some sort of plugin framework, so that package-A owners can provide an implementation to all users of library package-B without package-B having to depend on package-A

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

很糊涂小朋友 2025-01-23 18:47:47

OSGi 有一个服务注册表。 插件(捆绑包)可以在其接口名称下注册服务对象。其他插件可以获得该服务。例如,在 Tag.java 中,我们有:

  public interface Tag { void doThis(); }

服务的提供者可以提供如下实现

@Component
public class Foo implements Tag {
  @Override
  public void doThis() { System.out.println("do what I say"); }
}

@Component< /code> 注释由 OSGi 定义。当程序启动时,它会注册一个Tag服务。

下一部分是 Bar.java 中的消费者

@Component
public class Bar {
    @Activate public Bar( @Reference Tag tag) { tag.doThis(); }
}

@Component 将创建 Bar 的实例,并将将 Tag 服务的实例传递给构造函数,因为它具有 @Activate@Reference 注释。

作为提示,您希望将使用者、接口和提供者打包在三个不同的包中。

bndtools 视频 中展示了此模式的一个简单示例

OSGi has a service registry. A plugin (a bundle) can register a service object under its interface name. Other plugins can get that service. For example, in Tag.java we have:

  public interface Tag { void doThis(); }

The provider of the service can provide an implementation as follows:

@Component
public class Foo implements Tag {
  @Override
  public void doThis() { System.out.println("do what I say"); }
}

The @Component annotation is defined by OSGi. When the program starts, it will register a Tag service.

The next part is the consumer in Bar.java:

@Component
public class Bar {
    @Activate public Bar( @Reference Tag tag) { tag.doThis(); }
}

The @Component will create an instance of Bar and will pass an instance of the Tag service to the constructor because it has a @Activate and @Reference annotation.

As a tip, you want to package the consumer, the interface, and the provider in three different bundles.

A simple example of this pattern is shown in a bndtools video

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