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:
- 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!