cancel
Showing results for 
Search instead for 
Did you mean: 

How to override the implementation of a service

ioihanguren_
Star Contributor
Star Contributor

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,

1 ACCEPTED ANSWER

ioihanguren_
Star Contributor
Star Contributor

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!

View answer in original post

3 REPLIES 3

ioihanguren_
Star Contributor
Star Contributor

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!

Ok good to know thanks, that was surprising to me.

Thanks! It's good to know.