焊接 OSGi + Apache Felix = 找不到包

4 java osgi cdi jboss-weld jakarta-ee

我将 Apache Felix 和weld-osgi 用于Java SE 应用程序。问题是在注入的 bean 中,我使用@ApplicationScoped了 package javax.enterprise.context.ApplicationScoped。但是weld-osgi-bundle-2.1.2.Final.

这个包存在于weld-se但不在 OSGi 包中。我怎么解决这个问题?

pal*_*int 5

我会尝试将以下依赖项作为单独的包运行:

<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.1-20130918</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Maven 中央链接

小心,你需要 version 1.1-201309181.1 版MANIFEST.MF. 您可以解压缩jar并检查META-INF/MANIFEST.MF文件中的 OSGi 标头,如Bundle-ManifestVersionBundle-SymbolicName。您还可以在此处检查该捆绑包所需的包,它位于Import-Packages标题中。

如何弄清楚

检查weld-osgi-bundleMaven Central(或在其 中pom.xml)的依赖项。它包含以下内容:

<dependency>
    <groupId>org.jboss.weld</groupId>
    <artifactId>weld-api</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这个weld-api指的是cdi-api上面包含缺少的注释:

<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

另一种方法是在 Eclipse 中按 F3(打开声明),同时在ApplicationScoped注释中的光标然后在项目资源管理器视图中启用与编辑器链接,它将显示ApplicationScoped.classcdi-api-1.1.jar.

查找另一个 jars 的 OSGi 版本

您可能需要比这个更多的包(传递依赖或者它只是第一个停止安装的包)。并非所有知名的jar都有 OSGi 标头,如下所示:

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,在 Maven Central 上搜索组 ID。包含javax.inject包并具有 OSGi 标头的两个结果:

如果您找不到任何东西,您可以手动将任何 jar 转换为 OSGi 包。实际上,您可以通过weld-se.jar单独安装依赖项来做到这一点,看起来更干净。

  • 我已经在另一个问题上回答了它,如果您想将 CDI 与 OSGi 一起使用,请使用 Pax CDI。它与 OSGi 基金会合作,成为 OSGi 中 CDI 的参考实现。 (2认同)