Jonathan Huang
Jonathan Huang

Reputation: 3

can I provide and reference services in the same component when use declarative services for osgi?

<?xml version="1.0" encoding="UTF-8"?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="sensor_test"> <implementation class="test.sensor.version1.serviceImpl.SensorServiceImpl"/> <service> <provide interface="test.sensor.version1.serviceInterface.SensorServiceInterface"/> </service> <reference bind="setOntologyService" cardinality="0..1" interface="test.ontology.version1.serviceinterface.OntologyServiceInterface" name="OntologyServiceInterface" policy="dynamic" unbind="unsetOntologyService"/> </scr:component>

For what is showed above, is that OK to provide and reference services in the same component? For example, there are two bundles A and B. A provides a service for B to use, and B also provides a service for A to use. What should I do with this situation?

Upvotes: 0

Views: 292

Answers (1)

Neil Bartlett
Neil Bartlett

Reputation: 23958

This appears to be perfectly fine because the service reference is both optional and dynamic.

If you define a circular dependency where the references are both mandatory, then DS will report an error. There has to be a way to create one component first, and with mandatory references neither component can be created until the other is created (however, a third bundle "C" could register a service that satisfies one of the components).

With an optional/dynamic reference, DS can construct one component with the reference unbound, then construct the other component and inject it back into the first.

Upvotes: 1

Related Questions