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!

This entry was posted in e(fx)clipse. Bookmark the permalink.

6 Responses to Eclipse DI and OSGi-Services and dynamics

  1. Great feature! I’m playing with OSGi remote services (ECF) as part of a private home automation project and I am going to write a fx-desktop client as part of it, so this comes at a perfect timing. I should be able to inject the remote services when they are discovered :-).

  2. Interesting how every framework creates there own solution for this problem. Riena also has a mechanism for injecting multiple instances of the same service and I am sure others do that too. Personally I think that the use of several instances of the same service with different ranking is overrated but there might be some rare usecases….

    • Tom Schindl says:

      I have plenty of use for multi instances in my fx-ide research project but the annotation is used for more than getting this list injection stuff done! It’s there to align better with the Osgi dynamic

    • Veselin Markov says:

      Whiteboard pattern is a perfect example for the use of multiple service instances.

  3. Pingback: Making @Service annotation even cleverer | Tomsondev Blog

  4. Pingback: OSGi Declarative Services news in Eclipse Oxygen | vogella blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.