问题
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.
Well, I created:
- a Module (A) that contains two Interfaces (Prot,Com) and a class to show some GUI.
- a Module (B) that contains an implementation of interface Prot
- a Module (C) that contains an implementation of interface COM.
To register an implementation i used @ServiceProvider annotation. For Examble in Module B:
@ServiceProvider(service = Prot.class)
class ProtImpl implements Prot
{
@Override
...
}
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.
Lookup Examble in Module A:
private void printImplNames()
{
Prot prot = Lookup.getDefault().lookup(Prot.class);
prot.getName(); // <--- Null-Pointer
}
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.
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.
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.
Thanks ;)
回答1:
I've created a working example for this situation located at GitHub.
https://github.com/flinkgutt/stackoverflow/tree/master/LooseCoupling
来源:https://stackoverflow.com/questions/23476310/lookup-api-netbeans-platform-maven-loosely-coupled-modules