查找API NetBeans Platform + Maven:松散耦合的模块 [英] Lookup API NetBeans Platform + Maven: loosely coupled modules
问题描述
我正在开发NetBeans平台应用程序,并希望使用lookup api来获取特定接口的实现,同时仍然保持所有组件的松散耦合. 但是查询找不到任何服务提供商.
I'm developing a NetBeans platform application and want to use lookup api to get implementations for specific interfaces, still keeping everything loosely coupled. But the lookup can't find any service provider.
好,我创建了:
- 一个模块(A),其中包含两个接口(Prot,Com)和一个用于显示一些GUI的类.
- 包含接口Prot的实现的模块(B)
- 包含接口COM实现的模块(C).
要注册实现,我使用了 @ServiceProvider 批注. 对于模块B中的演说:
To register an implementation i used @ServiceProvider annotation. For Examble in Module B:
@ServiceProvider(service = Prot.class)
class ProtImpl implements Prot
{
@Override
...
}
如果您使用@ServiceProvider注释,则定义了默认构造函数. 我定义了对模块A的依赖关系,因为B需要了解有关接口的一些知识.
I defined the default-constructor, that is needed, if you use @ServiceProvider annotation. I defined a dependency on Module A because B need to know something about the interface.
模块A中的查找说明:
private void printImplNames()
{
Prot prot = Lookup.getDefault().lookup(Prot.class);
prot.getName(); // <--- Null-Pointer
}
现在,如果我查找接口Prot的实现,则会得到一个空指针异常.换句话说,没有注册实现.查找找不到任何服务提供商. 注释processcor可以正常工作,因为已在META-INF/.../目录中创建了文件.
Now if I look up the implementation of interface Prot, I get a null pointer exception. With other words, there is no implementation registered. The lookup cant find any service provider. Annotation processcor works fine, because files in META-INF/.../ directory were created.
唯一可行的方法是在模块A BUT 中定义对模块B的依赖关系,这样就失去了松散耦合模块的方面.
The only thing that seems to work is to define a dependency on Module B in Module A BUT in this way the aspect of loosley coupled modules is lost.
我遵循了NetBeans Platform网站和NetBeans Platform 7 Book上的指示,但似乎无济于事.希望有人能帮助我解决这个问题.
I followed intructions on NetBeans Platform Website and NetBeans Platform 7 Book but nothing seems to work. Hope somebody can help me to solve that problem.
谢谢;)
推荐答案
我在GitHub上针对这种情况创建了一个工作示例.
I've created a working example for this situation located at GitHub.
https://github.com/flinkgutt/stackoverflow/tree/master/LooseCoupling
这篇关于查找API NetBeans Platform + Maven:松散耦合的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!