我在 Eclipse Equinox OSGi 环境中使用 Apache Felix 服务组件运行时 (SCR)。
声明了几个实现接口的组件,org.example.Producer
例如:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerA">
<implementation class="org.example.ProducerA"/>
<service>
<provide interface="org.example.Producer"/>
</service>
</scr:component>
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerB">
<implementation class="org.example.ProducerB"/>
<service>
<provide interface="org.example.Producer"/>
</service>
</scr:component>
现在在另一个组件中,我想引用所有org.example.Producer
动态实现接口的组件:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ConsumerA">
<implementation class="org.example.ConsumerA"/>
<reference bind="bindProducer" cardinality="0..n" interface="org.example.Producer" policy="dynamic" unbind="unbindProducer"/>
<service>
<provide interface="org.example.Consumer"/>
</service>
</scr:component>
但这在运行时会出错。似乎 SCR 在其搜索过滤器中包含了一个组件名称:
!ENTRY org.eclipse.equinox.ds 1 0 2015-06-22 11:31:31.781
!MESSAGE Could not bind a reference of component org.example.ConsumerA. The reference is: Reference[name = org.example.Producer, interface = org.example.Producer, policy = dynamic, cardinality = 0..n, target = null, bind = bindProducer, unbind = unbindProducer]
正如您在错误消息中看到的那样,它正在显式搜索名称为 的组件org.example.Producer
。但是上面列出的组件都没有这个名称(org.example.ProducerA
, org.example.ProducerB
)。
所以问题是如何通过忽略名称来动态引用为给定接口提供实现的组件?