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:
- What happens if the there’s a newer higher ranked service registered?
- What if there are multiple services and you want all of them?
- What happens if the service is created with a OSGi-ServiceFactory on demand by the OSGi-Service registry?
The answer:
- Nothing will happen
- You can only inject the service with the highest ranking
- 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!
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 :-).
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….
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
Whiteboard pattern is a perfect example for the use of multiple service instances.
Pingback: Making @Service annotation even cleverer | Tomsondev Blog
Pingback: OSGi Declarative Services news in Eclipse Oxygen | vogella blog