java @Inject 字段始终为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30213584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:40:56  来源:igfitidea点击:

@Inject field is always null

javacdi

提问by Ben1980

I try to @Inject a field (its a jar module, empty beans.xml is existing under META-INF) like this:

我尝试 @Inject 一个字段(它是一个 jar 模块,空 beans.xml 存在于 META-INF 下),如下所示:

IDataProvider Interface

IDataProvider 接口

public interface IDataProvider {
  void test();
}

DataProvider implementation import javax.enterprise.context.ApplicationScoped;

DataProvider 实现 import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class DataProvider implements IDataProvider {
  private int i;

  public DataProvider() {
    i = 42;
  }

  @Override
  public void test() {

  }
}

And the class where i try to inject the DataProvider

以及我尝试注入 DataProvider 的类

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class DataController {

  @Inject
  private IDataProvider dataProvider;

  private int k;

  public DataController() {
     k = 42;
  }
}

If i run this on Wildfly the injected dataProvider is always null (Breakpoint at DataController constructor).

如果我在 Wildfly 上运行它,则注入的 dataProvider 始终为空(DataController 构造函数处的断点)。

On every tutorial it's done like this, so i thought this should work. Only difference is that both classes should be @ApplicationScoped

在每个教程中都是这样完成的,所以我认为这应该可行。唯一的区别是两个类都应该是@ApplicationScoped

I am using Wildfly 8.2Final, Gradle, IntelliJ. My gradle.build looks like this:

我正在使用 Wildfly 8.2Final、Gradle、IntelliJ。我的 gradle.build 看起来像这样:

apply plugin: 'java'

repositories {
  mavenCentral()
  jcenter()
}

dependencies {
  compile group:'javax', name:'javaee-web-api', version:'7.+'
  compile group:'org.jboss.ejb3', name:'jboss-ejb3-ext-api', version:'2.+'
}

sourceSets {
  main {
      java {
          srcDir 'src/api/java'
          srcDir 'src/main/java'
      }
  }

  test {
      java {
          srcDir 'src/test/java'
      }
  }
}

jar {
  from ('./src') {
      include 'META-INF/beans.xml'
  }
}

Does anyone have an idea why this is not working? i dont get any error or exception from Wildfly.

有谁知道为什么这不起作用?我没有从 Wildfly 收到任何错误或异常。

回答by Benjamin

During the construction of the DataController, it's normal the dataProvideris null. Injection happens after that.

在构造 的过程中DataControllerdataProvider为空是正常的。注射发生在此之后。

You should add a @PostConstructmethod to check for nullity of the field:

您应该添加一个@PostConstruct方法来检查字段是否为空:

@PostConstruct
void init() {
    // dataProvider should not be null here.
}

Aditionally, error reporting on Weld is pretty well done. So you should have an explicit and detailed error message if the injection fails, and not only a null field.

此外,关于 Weld 的错误报告做得非常好。因此,如果注入失败,您应该有明确且详细的错误消息,而不仅仅是空字段。