【问题标题】:Is there any way to make an adapter / wrapper pattern in annotations? [closed]有没有办法在注释中制作适配器/包装器模式? [关闭]
【发布时间】:2021-02-06 23:11:52
【问题描述】:

遵循一些说软件(尤其是业务逻辑)应该“独立于框架”的原则,我想知道是否可以在基于注释的库中应用一些包装器/适配器?

我们可以举个例子,Java 中的OSGi Declarative Services (DS) Annotations framewor,它使用注解来创建“组件”并使用、滥用依赖注入。

@Component(name = "Example", scope = ServiceScope.SINGLETON, service = ExampleSomething.class, immediate = true)
class Example implements ExampleSomething {
  @Override
  public void doSomething() {

  }
}

interface ExampleSomething {
  void doSomething();
}


@Component(name = "ExampleRef", scope = ServiceScope.SINGLETON, service = ExampleSomethingToRef.class, immediate = true)
class ExampleRef implements ExampleSomethingToRef {
  
  @Reference
  ExampleSomething exampleSomething;
  
  @Activate
  void onInit(Map<String, Object> properties) {
    
  }
  
  @Override
  public void makeSomething() {

  }
}

interface ExampleSomethingToRef {
  void makeSomething();
}

【问题讨论】:

  • Olviera 您可能想澄清您正在寻找什么。由于这些是构建时注释,它们没有运行时耦合。 IE。您可以在任何其他上下文中使用它们,并且这些组件无需运行 OSGi 即可测试。

标签: java design-patterns architecture osgi adapter


【解决方案1】:

您可以使用FindHook 隐藏您要包装的服务,然后发布具有与底层服务相同的属性/接口的包装器。

IIRC,OSGI 规范描述了这种情况:https://docs.osgi.org/specification/osgi.core/7.0.0/framework.servicehooks.html#d0e45668

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 2018-08-24
    • 2010-12-09
    • 1970-01-01
    • 2013-07-15
    • 2012-11-22
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多