是否可以使用 CDI 在没有生产者方法的情况下注入自定义 JBoss 模块?

Is it possible to inject a custom JBoss module without producer method using CDI?

我有一个提供服务的自定义 JBoss 7 模块(例如,EmailService 用于发送电子邮件)。我想在部署在同一个 AS 上的应用程序中使用这些服务。

我在 module.xml 中指定了服务的 jar(位于 modules/jboss/module/main)。

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="jboss.module">
  <resources>
    <resource-root path="email-service-api.jar" />
    <resource-root path="email-service-impl.jar" />
  </resources>
</module>

email-service-api.jar只包含服务的接口。我将其用作接口实现(在 email-service-impl.jar 中)和使用该服务的应用程序的依赖项。

email-service-impl.jar 中有一个名为 jboss.module.EmailService 的文件(在 META-INF/services 文件夹中)。该文件包含我所有实现的完全限定名称(到目前为止我只有一个):

jboss.module.impl.DefaultEmailService

我想将服务注入应用程序。

目前,我使用生产者方法从 应用。

package bean;

public class Bean {

    @Inject
    EmailService emailService;

    @Produces
    public EmailService getEmailService() {
        ServiceLoader<EmailService> emailServices = ServiceLoader.load(EmailService.class);

        for (EmailService emailService : emailServices) {
            if (emailService != null) {
                return emailService;
            }
        }

        return null;
    }
}

当我省略生产者方法时,我得到 org.jboss.weld.exceptions.DeploymentExceptionWELD-001408 Unsatisfied dependencies for type [EmailService] with qualifiers [@Default] at injection point [[field] @Inject bean.Bean.emailService]"}}

我在应用程序中有 jboss-deployment-structure.xml 个文件:

<jboss-deployment-structure>
  <deployment>
    <dependencies>
      <module name="jboss.module" services="export" />
    </dependencies>
  </deployment>
</jboss-deployment-structure>

并且我尝试将 beans.xml 文件添加到 "implementation-project"(即 email-service-impl.jar)中,但没有效果。异常还是发生了。

是否可以使用 CDI 注入服务并省略生产者方法?

谢谢,

丹尼斯

总结一下我在评论中写的内容:

  • 生产者可能是一种方式去这里。
    • 您正试图注入您首先需要加载的服务。 CDI 具有静态特性,只能注入在其启动期间可用的 bean。解决方法是使用生产者。
    • 要区分不同的实现,您可以使用限定符。例如。每个生产者和注入点都有一个给定的限定符(@DefaultImpl@ProUser 等)。
    • 可能 能够将这些生成器放入您的 API JAR 中,以免污染您的代码(加上空的 beans.xml)。
  • 至于生产者之外的其他方式
    • 我在这里只能想到 Extension,因为它们作为 CDI 引导执行,因此允许您将实现注册为 beans。
    • 这将使 bean 在 CDI 启动时可用,因此无需生产者即可注入。
    • 但是,事实证明这种方法要复杂得多。