Вопрос

I just started playing around with OSGi services and have the following situation. I have a project which contains 2 services. Service A requires Service B, so I tried to inject the dependent service using

@Inject
private ServiceB svc;

but the framework wont inject. If I setup the following two methods in Service A and set these methods as "bind / undbind" in my OSGi componentA.xml the framework calls these methods and I can use Service B in Service A.

public synchronized void bind(IServiceB service)
{
  this.svc = service;
}

public synchronized void unbind(IServiceB service)
{
  if (this.svc == service)
  {
    this.svc = null;
  }
}

The question is, why does it not work with @Inject ? Sorry if this is a stupid question, I'm quite new to this whole topic. Many thanks in advance!

Это было полезно?

Решение

It looks like you are using Declarative Services, which does not support field injection or the JSR-330 annotations. Field injection has limited utility in OSGi, where services may be injected or "un-injected" at any time. Method injection is more generally useful because it gives you an opportunity to do something when this happens.

However I do urge you to use the annotations for Declarative Services. This will save you from having to write the component.xml by hand.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top