创建 Java 插件
我有两个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OSGi 有一个服务注册表。 插件(捆绑包)可以在其接口名称下注册服务对象。其他插件可以获得该服务。例如,在
Tag.java
中,我们有:服务的提供者可以提供如下实现:
@Component< /code> 注释由 OSGi 定义。当程序启动时,它会注册一个
Tag
服务。下一部分是
Bar.java
中的消费者:@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:The provider of the service can provide an implementation as follows:
The
@Component
annotation is defined by OSGi. When the program starts, it will register aTag
service.The next part is the consumer in
Bar.java
:The
@Component
will create an instance ofBar
and will pass an instance of theTag
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