简体   繁体   English

在OSGi中使用声明性服务的Service Factory实现

[英]Service Factory implementation using declarative services in OSGi

I am registering an OSGi service using OSGi declarative service and setting 我正在使用OSGi声明性服务注册OSGi服务并进行设置

servicefactory="true" 服务工厂=“真”

as follows. 如下。

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="test.Configuration">
  <implementation class="test.ConfigurationImpl"/>
  <service servicefactory="true">
     <provide interface="test.Configuration"/>
  </service>
</scr:component>

I am trying to use the service multiple times from another bundle. 我正在尝试从另一个捆绑包中多次使用该服务。 But, it returns me the same instance of the service. 但是,它返回给我相同的服务实例。

What could possibly go wrong here ?? 这里可能出什么问题了?

The servicefactory flag means that each consumer bundle gets a separate instance. servicefactory标志意味着每个使用者捆绑包都有一个单独的实例。 So if you consume the service from 2 bundles then you will get 2 instances. 因此,如果您从2个捆绑中使用该服务,则将获得2个实例。 You do not get multiple instances per consumer. 每个使用者没有获得多个实例。

If you want programmatic control from the consumer over the number of instances then you need to use the ComponentFactory approach. 如果要让使用者通过程序控制实例的数量,则需要使用ComponentFactory方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM