The @Inject field is always zero

I am trying to @Inject a field (its module is jar, empty beans.xml exists in META-INF), like this:

IDataProvider Interface

public interface IDataProvider {
  void test();
}

Implementation of 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 in which I am trying to enter a 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 zero (breakpoint in the DataController constructor).

This is done in every tutorial, so I thought it should work. The only difference is that both classes must be @ApplicationScoped

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

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 do not get any errors or exceptions from Wildfly.

+4
source share
1

DataController, dataProvider . .

@PostConstruct :

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

, Weld . , , , .

+9

All Articles