Mar*_*tin 5 java lookup netbeans netbeans-platform
我需要有最简单的方法通过外部 jar 提供接口的实现。我想避免使用文本/xml 文件,因为在重构类或接口名称时它可能会导致错误。
我尝试了 Netbeans Lookup,因为它有一个@ServiceProvider 注释,应该以声明性方式注册实现。
所以我写了一个简单的试用代码,它甚至没有将接口和提供者拆分到不同的jar中,但它仍然无法查找组件:
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;
public class LookupTrial {
public static void main(String[] args) {
IService s = Lookup.getDefault().lookup(IService.class);
if(s==null)
throw new RuntimeException("lookup failed");
else
s.hello();
}
public interface IService{
public void hello();
}
@ServiceProvider(service=IService.class)
public class MyImplementation implements IService{
public MyImplementation(){}
@Override
public void hello() {
System.out.println("hello lookup");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我是否错误地使用了Lookup?我应该搜索另一个查找库来执行我想要的操作吗?