Blog

Eclipse DI and OSGi-Services and dynamics

Most people who use Eclipse DI know that they can simply use

@Inject
MyOsgiService service;

and the system will inject the service as desired but there are multiple problems with that:

  1. What happens if the there's a newer higher ranked service registered?
  2. What if there are multiple services and you want all of them?
  3. What happens if the service is created with a OSGi-ServiceFactory on demand by the OSGi-Service registry?

The answer:

  1. Nothing will happen
  2. You can only inject the service with the highest ranking
  3. The service is always requested with the BundleContext of one of the framework bundles

With the next nightly build of e(fx)clipse - users of our core-bundles (you not necessarily need to write e4+JavaFX apps!) can now overcome those limitations with this:

@Inject
@Service
MyOsgiService service; // highest ranked service

@Inject
@Service
List<MyOsgiService> service; // services sorted by ranking

This simple annotation fixes all your problems!

published on
Previous
Displaying and editing large (styled) texts