Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2.0 Constructor injection and Singleton

is it possible to use Dagger 2.0 Constructor injection and singleton at one time. I don't find the answer in the documentation.

Example:

@Singleton
public class MyClass {
   private final OtherClass member;

   @Inject
   public MyClass(OtherClass member){
        this.member = member;
   }
 }

Constructor injection does work for sure. But is it guaranteed that MyClass is created as a singleton if I write @Singleton on the class?

Thank you

like image 922
Soccertrash Avatar asked May 20 '15 17:05

Soccertrash


1 Answers

Yes.

Since Dagger 2 generates the source code for you, it is easy to inspect what is happening. For example, when using the following module in combination with your MyClass:

@Component
@Singleton
public interface MyComponent {
  MyClass myClass();
}

The following implementation is generated:

@Generated("dagger.internal.codegen.ComponentProcessor")
public final class DaggerMyComponent implements MyComponent {
  private Provider<MyClass> myClassProvider;

  private DaggerMyComponent(Builder builder) {  
    assert builder != null;
    initialize(builder);
  }

  public static Builder builder() {  
    return new Builder();
  }

  public static MyComponent create() {  
    return builder().build();
  }

  private void initialize(final Builder builder) {  
    this.myClassProvider = ScopedProvider.create(MyClass_Factory.create(OtherClass_Factory.create()));
  }

  @Override
  public MyClass myClass() {  
    return myClassProvider.get();
  }

  public static final class Builder {
    private Builder() {  
    }

    public MyComponent build() {  
      return new DaggerMyComponent(this);
    }
  }
}

In initialize(Builder), you can see that a ScopedProvider is used as a Provider for MyClass. When calling the myClass() method, the ScopedProvider's get() method is called, which is implemented as a singleton:

public T get() {
  // double-check idiom from EJ2: Item 71
  Object result = instance;
  if (result == UNINITIALIZED) {
    synchronized (this) {
      result = instance;
      if (result == UNINITIALIZED) {
        instance = result = factory.get();
      }
    }
  }
  return (T) result;
}
like image 116
nhaarman Avatar answered Oct 28 '22 02:10

nhaarman