在我们的项目(Osgi、Spring-Dm、Apache-Felix)中,当 BundleContext.getService() 返回 null 时,我们在生产中遇到了一些问题。 它很少发生,而且一开始也不会发生,因此这不是配置错误问题。
另一种方法是使用 ServiceTracker 来定位服务,但由于错误很少见,我不确定更改它是否可以解决问题。
最佳答案
OSGi 规范说该方法返回
- A service object for the service associated with reference
- or null if
- the service is not registered
- the service object returned by a ServiceFactory does not implement the classes under which it was registered
- the ServiceFactory threw an exception
我的第一个猜测是该服务未注册。
顺便说一下,使用 Spring-DM 应该可以使大多数对服务注册表的编程访问变得不必要。至少在我们公司,这些调用通常是被禁止的,只允许在较低级别的技术代码中使用。
关于java - Osgi getService(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266254/