dagger2探索

网上关于Dagger2的博客很多,但自己还是觉得大多都讲得稀里糊涂,大多只讲了怎么用,但始终是没怎么讲其内部实现流程,懒得搜博客,干脆直接看源码。

简单的一个应用

首先从一个最最简单的例子开始
一个user类,在其构造方法加个Inject注解,功能相当于表示此处提供User的实例化对象

public class User {
private String userName;
    private String password;

    @Inject
    public User() {
    // 暂时用无参,便于分析
        this.userName = " dfs";
        this.password = "fdsa ";
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

然后来个随意的注射器MainActivityComponent,@Component()表示此接口为注射器,这里里面有一个往MainActivity注射的方法

@Component()
public interface MainActivityComponent {
    void inject(MainActivity mainActivity);
}

然后make下model,生成DaggerMainActivityComponent类,最后应用于MainActivity

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity>>>";
    @Inject
    User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DaggerMainActivityComponent.builder()
                .build()
                .inject(this);
        Log.i(TAG, "onCreate: >>>>" + user);
    }
}

得到结果,显而易见是注射成功了

I/MainActivity>>>: onCreate: >>>>User{userName=' dfs', password='fdsa '}

源码分析

User类的构造方法加了注解Inject后,后面编译时会生成一个对应的枚举

@Generated("dagger.internal.codegen.ComponentProcessor")
public enum User_Factory implements Factory<User> {
INSTANCE;

  @Override
  public User get() {  
    return new User();
  }

  public static Factory<User> create() {  
    return INSTANCE;
  }
}

看了这个,我才知道枚举也能像类一样。。。之前看到书上说枚举可以实现单例,还说是最安全的,但书上却没有给出例子,这里应该就是枚举实现单例的一个应用。对于这个枚举的理解就是工厂,专门生产实例
然后看下Factory这个接口,很简单,就是继承Provider接口

public interface Factory<T> extends Provider<T> {
}

provider里面呢,里面就一个get方法

public interface Provider<T> {
    T get();
}

然后MainActivity用了@Inject注解,然后我看到debug对应的目录下生成了MainActivity_MembersInjector类


@Generated("dagger.internal.codegen.ComponentProcessor")
public final class MainActivity_MembersInjector implements MembersInjector<MainActivity> {
  private final MembersInjector<AppCompatActivity> supertypeInjector;
  private final Provider<User> userProvider;

  public MainActivity_MembersInjector(MembersInjector<AppCompatActivity> supertypeInjector, Provider<User> userProvider) {  
    assert supertypeInjector != null;
    this.supertypeInjector = supertypeInjector;
    assert userProvider != null;
    this.userProvider = userProvider;
  }

  @Override
  public void injectMembers(MainActivity instance) {  
    if (instance == null) {
      throw new NullPointerException("Cannot inject members into a null reference");
    }
    supertypeInjector.injectMembers(instance);
    instance.user = userProvider.get();
  }

  public static MembersInjector<MainActivity> create(MembersInjector<AppCompatActivity> supertypeInjector, Provider<User> userProvider) {  
      return new MainActivity_MembersInjector(supertypeInjector, userProvider);
  }
}

MainActivity_MembersInjector实现了接口MembersInjector里的injectMembers方法,先调用了supertypeInjector.injectMembers(instance)这里应该有个逐级递归,然后 instance.user = userProvider.get()把user实例赋给MainActivity的user成员变量。对MainActivity_MembersInjector理解就是用来具体实现注入的,并且可以注入多个成员

然后MainActivityComponent也对应生成了一个DaggerMainActivityComponent,其实是对MainActivityComponent的进一步封装


@Generated("dagger.internal.codegen.ComponentProcessor")
public final class DaggerMainActivityComponent implements MainActivityComponent {
  private MembersInjector<MainActivity> mainActivityMembersInjector;
  private DaggerMainActivityComponent(Builder builder) {  
    assert builder != null;
    initialize(builder);
  }

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

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

  private void initialize(final Builder builder) {  
    this.mainActivityMembersInjector = MainActivity_MembersInjector.create((MembersInjector) MembersInjectors.noOp(), User_Factory.create());
  }

  @Override
  public void inject(MainActivity mainActivity) {  
    mainActivityMembersInjector.injectMembers(mainActivity);
  }

  public static final class Builder {
    private Builder() {  
    }
  
    public MainActivityComponent build() {  
      return new DaggerMainActivityComponent(this);
    }
  }
}

DaggerMainActivityComponent给MainActivityComponent加了个Builder,典型运用了Builder模式,便于链式调用,设置参数。这里Builder构造了MembersInjector<MainActivity>这个类型的成员,然后DaggerMainActivityComponent就可以利用mainActivityMembersInjector这个实例去给User变量注入实例。

扩展,User设置为单例注入

上述只是一种场景,注射器每次注入的都是一个全新的对象,并不是同一个对象,接下看看dagger是怎么实现单例注入的

简单应用,在前面代码的基础上

在前面的User类上加了@Singleton

@Singleton
public class User {
    ...
}

然后MainActivityComponent加了个@Singleton注解

@Singleton
@Component
public interface MainActivityComponent {
void inject(MainActivity mainActivity);
}

最后编译一下,应用

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity>>>";
    @Inject
    User user1;
    @Inject
    User user2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DaggerMainActivityComponent.builder()
                .build()
                .inject(this);

        Log.i(TAG, "onCreate: >>>>user1: " + user1.hashCode()+"  user2:"+user2.hashCode());
    }
}

结果:

onCreate: >>>>user1: 219956319  user2:219956319

可以看到user1和user2是同一个对象

可是为什么加两个注解就可以实现单例注入呢!!
直接看生成的代码和前一次的有什么不同

其实不同点就在于DaggerMainActivityComponent 里面的initialize方法


@Generated("dagger.internal.codegen.ComponentProcessor")
public final class DaggerMainActivityComponent implements MainActivityComponent {
  private Provider<User> userProvider;
  private MembersInjector<MainActivity> mainActivityMembersInjector;

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

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

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

  private void initialize(final Builder builder) {  
    this.userProvider = ScopedProvider.create(User_Factory.create());
    this.mainActivityMembersInjector = MainActivity_MembersInjector.create((MembersInjector) MembersInjectors.noOp(), userProvider);
  }

  @Override
  public void inject(MainActivity mainActivity) {  
    mainActivityMembersInjector.injectMembers(mainActivity);
  }

  public static final class Builder {
    private Builder() {  
    }
  
    public MainActivityComponent build() {  
      return new DaggerMainActivityComponent(this);
    }
  }
}

构造mainActivityMembersInjector需要传入userProvider,原来的userProvider是直接User_Factory.create()生成,然而现在mainActivityMembersInjector构造时传入的userProvider是经ScopedProvider.create封装过的userProvider,所以单例实现关键就是在里面

看一下ScopedProvider的源码

public final class ScopedProvider<T> implements Provider<T> {
  private static final Object UNINITIALIZED = new Object();

  private final Factory<T> factory;
  private volatile Object instance = UNINITIALIZED;

  private ScopedProvider(Factory<T> factory) {
    assert factory != null;
    this.factory = factory;
  }

  @SuppressWarnings("unchecked") // cast only happens when result comes from the factory
  @Override
  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;
  }

  /** Returns a new scoped provider for the given factory. */
  public static <T> Provider<T> create(Factory<T> factory) {
    if (factory == null) {
      throw new NullPointerException();
    }
    return new ScopedProvider<T>(factory);
  }
}

原来里面用了个单例模式,经典的双重检查DCL双重检查锁定,这样provider提供的实例就是同一个对象了

总结

通过这两个例子,大致可以知道Dagger实现注入的基本流程,首先声明的注解会对应生成对应的工具类,如xxx_Factory工厂类,xxx_MembersInjector成员注入工具类,DaggerXXXComponet注射器类,xxx_Factory提供对象实例,xxx_MembersInjector是将xxx_Factory提供的实例赋给目标变量,DaggerXXXComponet起连接作用,是目标类和注入工具类的中介者。

当然Dagger2还有其它用法,如Module的使用,但我粗虐的实验了下,其中的流程也基本一样,xxx_Factory类型由枚举enum变成了class,其它不同点还有待细看

还有个Scope注解我还没弄懂,有时间继续探索。。。

第一次写源码分析的文章,语言组织可能不尽人意,以后会慢慢提高。。。

上一篇:一位android开发程序猿的小工具集合


下一篇:databinding学习