Field @Inject not working in Dagger2

2019-08-26 10:39发布

问题:

I am new to CDI with Dagger. I have the following structure. The issue is when I go to fetch backendService in class Main, only the direct backendService is fetched, but the underlying User dependency remains null. Is there anything wrong with this setup.

Class MyComponent

import javax.inject.Singleton;
import dagger.Component;

@Singleton
@Component(modules = {UserModule.class, BackEndServiceModule.class})
public interface MyComponent {

  User user();

  BackendService backendService();

  void inject(Main main);
  void injectIntoBackendService(BackendService backendService);
}

Class User:

import javax.inject.Inject;

public class User {
  private String firstName;
  private String lastName;

  public User(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return "User [firstName=" + firstName + ", lastName=" + lastName + "]";
  }
}

Class UserModule

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module
public class UserModule {
  @Provides @Singleton User providesUser() {
    return new User("Lars", "Vogel");
  }
}

BackendService

import javax.inject.Inject;
import javax.inject.Named;

public class BackendService {

  @Inject public User user;

  private String serverUrl;

  @Inject
  public BackendService(@Named("serverUrl") String serverUrl) {
    this.serverUrl = serverUrl;
  }

  public boolean callServer() {
    System.out.println("User: " + user);
    if (user !=null && serverUrl!=null && serverUrl.length()>0) {
      System.out.println("User: " + user + " ServerUrl: "  + serverUrl);
      return true;
    }
    return false;
  }
}

BackEndServiceModule

import javax.inject.Named;
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module
public class BackEndServiceModule {

  @Provides
  @Singleton
  BackendService provideBackendService(@Named("serverUrl") String serverUrl) {
    return new BackendService(serverUrl);
  }

  @Provides
  @Named("serverUrl")
  String provideServerUrl() {
    return "http://www.vogella.com";
  }

  @Provides
  @Named("anotherUrl")
  String provideAnotherUrl() {
    return "http://www.google.com";
  }
}

Main

import javax.inject.Inject;

public class Main {

  @Inject public BackendService backendService;

  public void callService() {
    boolean callServer = backendService.callServer();

    if (callServer) {
      System.out.println("Server call was successful. ");
    } else {
      System.out.println("Server call failed. ");
    }
  }

  public static void main(String[] args) {
    Main m = new Main();
    MyComponent component = DaggerMyComponent.builder().build();
    component.inject(m);
    m.callService();
  }
}

回答1:

Please have a look at field injection vs constructor injection. You try to use both partially, ending up to use neither.

@Singleton
@Component(modules = {UserModule.class, BackEndServiceModule.class})
public interface MyComponent {

  User user();

  BackendService backendService();

  void inject(Main main);

  // you don't need that if you create the object!
  // void injectIntoBackendService(BackendService backendService);
}

When in doubt, use constructor injection. As the name suggests..define dependencies in the constructor and get rid of any other @Inject.

@Singleton // add singleton here, use constructor injection!
public class BackendService {

  public User user;

  private String serverUrl;

  @Inject
  public BackendService(@Named("serverUrl") String serverUrl, User user) {
    this.serverUrl = serverUrl;
    this.user = user;
  }
}

And when you don't need any further setup you don't need any @Provides annotated methods either, so get rid of provideBackendService(@Named("serverUrl") String serverUrl) please. We specified the scope on the implementation class above.

I also wrote an article about a few basic things to keep in mind when using Dagger.