有没有办法在注释中制作适配器/包装器模式?

Is there any way to make an adapter / wrapper pattern in annotations?

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

我们可以举个例子,在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();
}

您可以使用 FindHook 来隐藏您想要包装和发布的服务,而不是使用与基础服务相同 properties/interfaces 的包装器。

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