我需要创建具有相同接口的服务组件。这意味着我对相同的接口有不同的实现。我试图创建两个具有相同界面的组件,其中一个仅处于活动状态。
我正在使用Equinox声明。你有更好的设计来解决这个问题吗?请在下面找到我的配置。
组件1.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="comp1">
<implementation class="com.demo.impl.CompOneImpl"/>
<service>
<provide interface="com.demo.IComponent"/>
</service>
</scr:component>
组件2.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="comp2">
<implementation class="com.demo.impl.CompTwoImpl"/>
<service>
<provide interface="com.demo.IComponent"/>
</service>
</scr:component>
从消费者访问组件
消费组件
comp1.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="consumeComp1">
<implementation class="com.demo.service.ConsumeCompOne"/>
<reference bind="setComp" cardinality="1..1" interface="com.demo.IComponent" name="comp1" policy="static" unbind="unsetComp"/>
</scr:component>
comp2.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="consumeComp2">
<implementation class="com.demo.service.ConsumeCompTwo"/>
<reference bind="setComp" cardinality="1..1" interface="com.demo.IComponent" name="comp2" policy="static" unbind="unsetComp"/>
</scr:component>
当我尝试通过 ConsumeCompOne 和 ConsumeCompTwo 类分别访问 comp1 和 comp2 时,我总是得到相同的组件,无论是 comp1 还是 comp2。请帮我解决这个问题。
先感谢您
牧牛姑娘