OSGI @Component 注释在扩展现有 OSGI 服务时不包含基础 class 所需的引用
OSGI @Component annotation does not include references required by the base class while extending an existing OSGI service
我正在尝试扩展 OSGI 服务。正在扩展的 OSGI 服务包括一些引用和属性。我正在使用新的 org.osgi.service.component.annotations
包。 OSGi R6 实现的注解处理器生成的元 XML 不考虑我正在扩展的 OSGI 服务中的引用和 属性 声明。
Apache Felix Maven SCR 插件可以很好地处理这个用例,并且用 Felix 注释注释的 class 也包括基础 class 的引用和属性。
有没有办法让它与官方 OSGI 注释实现一起工作。我不想回退到 Felix SCR 插件,除非我必须按照他们的官方网站所说的继续 OSGI 实现,这是一个尚未使用 SCR 插件的新项目。
The meta XML generated by the annotations processor of OSGi R6 implementation does not account for the reference and property declarations made in the OSGI service I'm extending.
您期望的行为取决于您用来生成 XML 的构建工具,而不是注释本身。一般来说,根据 parent class 中的注释生成 XML 并不是一个好主意。这是因为构建时定位的 parent class 可能与运行时定位的 parent class 不同。在这种情况下,生成的注入站点可能在运行时实际上不存在,从而导致很多问题。事实上,即使类型相同,您也是从子 class.
引用 parent class 的私有详细信息
撇开这个警告不谈,您可能正在使用 bnd-based 工具,例如 maven-bundle-plugin
或 bnd-maven-plugin
来生成 XML 文件。为了避免我提到的问题,bnd 不会在组件的 parent class 中搜索注解,但可以使用以下指令在配置中覆盖此行为:
-dsannotations-options: inherit
如果您添加该配置选项,那么您应该会看到您想要的行为,但是 强烈 建议您在 parent class 和 child class 在不同的包中。
为了在maven中使用你可以这样定义:
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>run-bnd</id>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
<configuration>
<bnd><![CDATA[-dsannotations-options: inherit]]></bnd>
</configuration>
</plugin>
</plugins>
另一个不需要从基础继承注释的选项 class 是在组件本身中重新声明所需的引用:
@Component(
service = SomeService.class,
reference = {
@Reference(
name = "baseClassField",
field = "baseClassField",
service = SomeOtherService.class
),
@Reference(
name = "otherBaseClassField",
field = "otherBaseClassField",
service = YetAnotherService.class,
cardinality=ReferenceCardinality.MULTIPLE,
policy=ReferencePolicy.DYNAMIC
)
}
)
public class MyServiceImplementation
extends AbstractSomeServiceBaseImpl
implements SomeService
{...}
明显的缺点是您显式硬编码了 superclass 的实现细节,这可能比在构建时隐式继承它们更错误。这样,运行时 class 不仅可以具有与编译时依赖项不同的字段,而且即使在基础 class 更改的编译时,您也必须确保更新 class 以反映添加、删除或重命名的字段。
我正在尝试扩展 OSGI 服务。正在扩展的 OSGI 服务包括一些引用和属性。我正在使用新的 org.osgi.service.component.annotations
包。 OSGi R6 实现的注解处理器生成的元 XML 不考虑我正在扩展的 OSGI 服务中的引用和 属性 声明。
Apache Felix Maven SCR 插件可以很好地处理这个用例,并且用 Felix 注释注释的 class 也包括基础 class 的引用和属性。
有没有办法让它与官方 OSGI 注释实现一起工作。我不想回退到 Felix SCR 插件,除非我必须按照他们的官方网站所说的继续 OSGI 实现,这是一个尚未使用 SCR 插件的新项目。
The meta XML generated by the annotations processor of OSGi R6 implementation does not account for the reference and property declarations made in the OSGI service I'm extending.
您期望的行为取决于您用来生成 XML 的构建工具,而不是注释本身。一般来说,根据 parent class 中的注释生成 XML 并不是一个好主意。这是因为构建时定位的 parent class 可能与运行时定位的 parent class 不同。在这种情况下,生成的注入站点可能在运行时实际上不存在,从而导致很多问题。事实上,即使类型相同,您也是从子 class.
引用 parent class 的私有详细信息撇开这个警告不谈,您可能正在使用 bnd-based 工具,例如 maven-bundle-plugin
或 bnd-maven-plugin
来生成 XML 文件。为了避免我提到的问题,bnd 不会在组件的 parent class 中搜索注解,但可以使用以下指令在配置中覆盖此行为:
-dsannotations-options: inherit
如果您添加该配置选项,那么您应该会看到您想要的行为,但是 强烈 建议您在 parent class 和 child class 在不同的包中。
为了在maven中使用你可以这样定义:
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>run-bnd</id>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
<configuration>
<bnd><![CDATA[-dsannotations-options: inherit]]></bnd>
</configuration>
</plugin>
</plugins>
另一个不需要从基础继承注释的选项 class 是在组件本身中重新声明所需的引用:
@Component(
service = SomeService.class,
reference = {
@Reference(
name = "baseClassField",
field = "baseClassField",
service = SomeOtherService.class
),
@Reference(
name = "otherBaseClassField",
field = "otherBaseClassField",
service = YetAnotherService.class,
cardinality=ReferenceCardinality.MULTIPLE,
policy=ReferencePolicy.DYNAMIC
)
}
)
public class MyServiceImplementation
extends AbstractSomeServiceBaseImpl
implements SomeService
{...}
明显的缺点是您显式硬编码了 superclass 的实现细节,这可能比在构建时隐式继承它们更错误。这样,运行时 class 不仅可以具有与编译时依赖项不同的字段,而且即使在基础 class 更改的编译时,您也必须确保更新 class 以反映添加、删除或重命名的字段。