【问题标题】:Constructor injection with wildfly 9 not workingWildfly 9的构造函数注入不起作用
【发布时间】:2016-02-02 01:54:19
【问题描述】:

我正在尝试在 wildfly 9.0.2 final 上部署一个简单的 WAR 文件。 WAR 目前没有 web.xml 和 EJB——只是通过注释,一个 websocket 端点,在构造函数和工厂中具有依赖注入以产生依赖。在部署时,我收到错误:

java.lang.NoSuchMethodException: com.aip.textspace.infra.control.websocket.MainEndPoint.<init>

widlfly 似乎试图调用一个不存在的 null 构造函数。这是 MainEndPoint 的构造函数:

@Inject
public MainEndPoint(SessionController control) {
    super();
    this.control = control;
}

我有另一个名为 SessionControllerFactory 的类,它有一个用于注入的生产者方法:

@Produces
public SessionController build() {
...

在 Maven POM 中,我引用了 CDI 库:

   <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
    </dependency>

那么我遗漏了 wildfly 使用依赖注入调用构造函数所需的什么?

顺便说一句,基于related response,我尝试向 MainEndPoint 添加一个空构造函数。在这种情况下,部署成功但注入永远不会发生,并且在 MainEndPoint 中访问 this.control 时出现空指针异常。

【问题讨论】:

  • 你能提供更多你的MainEndPoint的定义吗?

标签: jakarta-ee dependency-injection cdi wildfly


【解决方案1】:

原因似乎很明显:不知何故,SessionController 尚未注册为 CDI bean。

请确保在以下代码中:

 @Produces
 public SessionController build() {
    ...
 }

注释@Produces 声明@javax.enterprise.inject.Produces,而不是@javax.ws.rs.Produces

另外请确保 SessionControllerFactory 包含在您的战争档案中,并且它是一个托管 bean 类

顺便说一句:您没有提及您的 beans.xml 文件(这在 WildFly 9 和一般的 EE 7 中是可选的)。出于测试目的,您可以添加此类文件,设置其属性bean-discovery-mode="ALL" 并检查会发生什么。

【讨论】:

  • 我需要创建工厂类(SessionControllerFactory 及其构造所需的工厂)@Singleton EJB,并为它们添加无用的 null 构造函数。现在完美运行。暂时不需要 beans.xml。
猜你喜欢
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多