OSGi dependencies can be used to inject implementations of some types, like BarImpl
class implementing Bar
interface which can be injected to @Reference Bar bar
field. Optional dependencies mean that your OSGi component can use some service, but doesn't require it to work.
However, if OSGi doesn't know type you are trying to use as a field, you will get an exception and it is a valid behaviour. You simply can't use a class if its field has unknown type - and its true not only for OSGi but for Java in general.
A good approach here is to split bundle containing Bar
implementation to two bundles:
bar-api
containingBar
interface,bar-impl
containing OSGi serviceBarImpl
.
bar-api
is still required by FooImpl
, but bar-impl
is truly optional and disabling it in the Felix console won't break referencing components.