【发布时间】: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