当为 osgi 使用声明性服务时,我可以在同一组件中提供和引用服务吗?
如上所示,在同一个组件中提供和引用服务可以吗? 例如,有两个bundle A和B,A提供服务供B使用,B也提供服务供A使用。遇到这种情况我该怎么办?
<?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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来完全没问题,因为服务引用既是可选的又是动态的。
如果您定义了一个循环依赖关系,其中引用都是强制的,那么 DS 将报告错误。必须有一种方法首先创建一个组件,并且通过强制引用,在创建另一个组件之前,两个组件都无法创建(但是,第三个捆绑包“C”可以注册满足其中一个组件的服务)。
通过可选/动态引用,DS 可以构造一个未绑定引用的组件,然后构造另一个组件并将其注入回第一个组件。
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.