【问题标题】:Is a declarative service a singleton by default?默认情况下,声明式服务是单例吗?
【发布时间】:2015-10-22 16:24:04
【问题描述】:

配置:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true" immediate="true" name="printing">
   <implementation class="printing.PrinterManager"/>
   <service>
      <provide interface="service.printing.IPrinterManager"/>
   </service>
</scr:component>

班级:

public class PrinterManager implements IPrinterManager {

    public void activate(BundleContext ctx) {
        System.err.println("hello");
    }

    public void deactivate(BundleContext ctx) {
        System.err.println("bye");
    }
}

当我像这样在代码中的其他地方注入该类的对象时:

@Inject
IPrinterManager pm;

我是否总是得到该类的相同实例? 如果它还不是单例,如何在 osgi/equinox 设施的帮助下使该实例成为单例? (我知道如何用 java/vanilla 方式编写单例,这不是我的问题)

【问题讨论】:

  • @Inject?这不应该是@Reference 吗?
  • 我想这取决于您选择的 DI 框架。 @Inject 在 Equinox/e4/javafx 星座中对我有用

标签: osgi equinox declarative-services


【解决方案1】:

是的,默认情况下 DS 组件是单例的。见Multiple Component Instances with OSGi Declarative Services

【讨论】:

  • 感谢您的回答,您介意分享一个指向您的知识来源的链接吗?
  • 添加了答案的链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 2018-07-11
  • 2012-10-01
  • 2019-05-21
  • 2015-05-28
  • 1970-01-01
相关资源
最近更新 更多