How to override the implementation of a service
Hi,
I have a component that declares a service. The service has different implementations provided in separate bundles:
- BaseBundle:
- ServiceInterface
- BaseServiceImpl
- SecondBundle:
- SecondServiceImpl
- ThirdBundle:
- ThirdServiceImpl
The service and implementations are declared in a contribution in each bundle, like this:
// BaseBundle
<?xml version="1.0"?>
<component name="BaseBundleComponent">
<implementation class="org.example.service.impl.BaseServiceImpl"/>
<service>
<provide interface="org.example.service.ServiceInterface"/>
</service>
</component>
// SecondBundle
<?xml version="1.0"?>
<component name="SecondBundleComponent">
<require>BaseBundleComponent</require>
<implementation class="org.example.service.impl.SecondServiceImpl"/>
<service>
<provide interface="org.example.service.ServiceInterface"/>
</service>
</component>
// ThirdBundle
<?xml version="1.0"?>
<component name="ThirdBundleComponent">
<require>SecondBundleComponent</require>
<implementation class="org.example.service.impl.ThirdServiceImpl"/>
<service>
<provide interface="org.example.service.ServiceInterface"/>
</service>
</component>
So, the SecondBundle requires the BaseBundle (because it extends BaseBundle implementation). And the ThirdBundle requires the SecondBundle (for the same reason). I thought that with the requires I would be able to override the implementation of ServiceInterface:
- If I load Base + Second, the implementation should be SecondServiceImpl
- If I load Base + Second + Third, the implementation should be ThirdServiceImpl
But I've seen that it is not that way.
So what is the correct way to override the implementation of a service depending on the bundles that are loaded? Is it possible?
Thank you in advance,
As it turns out, I had a typo in the contribution of the third bundle, and that was the reason why it didn't work. So, using different implementations with requires actually works in my case.
Sorry for the mistake!
Note that even though you can override a service, the extension point contributions are made to a component and not to a service, so your new implementation will need its own extension points, or to look up the ones that have been made to the old service.