Android 神兵利器Dagger2使用详解(四)Scope注解的使用及源码分析

前言

本系列所有文章:

Android 神兵利器Dagger2使用详解(一)基础使用
Android 神兵利器Dagger2使用详解(二)Module&Component源码分析
Android 神兵利器Dagger2使用详解(三)MVP架构下的使用
Android 神兵利器Dagger2使用详解(四)Scope注解的使用及源码分析
告别Dagger2模板代码:DaggerAndroid使用详解
告别Dagger2模板代码:DaggerAndroid原理解析
该系列首发于我的CSDN专栏 :
Android开发:Dagger2详解

在我的上一篇文章中,我们以简单的案例对Dagger2依赖注入库在实际开发中的使用方法进行了学习。

本文内容:

1.@Singleton 全局单例 注解的使用

2.自定义@Scope 局部单例 注解的使用

3.通过 **源码分析 **@Singleton和@Scope注解是 如何实现单例 的。

一.@Singleton:全局单例注解

承接上文,我们在AppModule中提供了以下对象的初始化:

@Module
public class AppModule {

    private MyApplication application;

    public AppModule(MyApplication application) {
        this.application = application;
    }

    //提供sp对象
    @Provides
    SharedPreferences provideSharedPreferences(){
        return application.getSharedPreferences("spfile", Context.MODE_PRIVATE);
    }

    //提供Application对象
    @Provides
    MyApplication provideApplication(){
        return application;
    }
}

我们肯定会有这样一种需求,类似于SharedPreferences对象,我们可能在整个App的生命周期中只需要一个单例,而不需要每次都通过application.getSharedPreferences("spfile", Context.MODE_PRIVATE)获得新的对象,那么怎么办呢,我们只需要在你需要单例的对象提供方法上加一个注解@Singleton ,类似这样:

@Module
public class AppModule {

    private MyApplication application;

    public AppModule(MyApplication application) {
        this.application = application;
    }
    
    //全局单例SharedPreferences
    @Provides
    @Singleton
    SharedPreferences provideSharedPreferences() {
        return application.getSharedPreferences("spfile", Context.MODE_PRIVATE);
    }

    @Provides
    MyApplication provideApplication() {
        return application;
    }
 }

只需要一个@Singleton,无论我们在多少个Activity容器中通过@Inject获取这个SharedPreferences对象的实例,该对象都是同一个对象,从而实现了全局单例的效果。

非常简单的使用方式,你需要什么对象全局单例,就在提供该对象方法的@Provides注解旁加上一个@Singleton,并且在该module关联的Component中加上同样的注解

@Singleton      //不要忘了还要在关联的Component接口中声明,否则会编译报错
@Component(modules = AppModule.class)
public interface AppComponent {

    SharedPreferences sharedPreferences();

    MyApplication myApplication();

}

ok,使用方式很简单。

二.自定义@Scope 局部单例注解

事实上,除了全局单例,我们可能在开发中更多的是需要局部单例,比如,我在ActivityA中需要实例化两个Student对象,在ActivityB中也需要一个Student对象,但我希望ActivityA中的两个Student都叫小明,但ActivityB中的Student对象叫小刚。

这样的需求,全局单例注解@Singleton是不行的,因为如果在AppModule中通过@Singleton注解提供Student,两个Activity中的Student都是小明(全局单例)了,而如果不用@Singleton注解,那么两个Activity中的Student是三个不同的对象,这样又不满足“ActivityA中的两个Student都叫小明”的需求。

这时我们就需要自定义@Scope注解实现局部单例了。

1.自定义@Scope注解

实现方式很简单,首先这样自定义一个接口ActivityScope,声明这个注解可以使对象在同一个Activity中实现单例:

@Scope  //声明这是一个自定义@Scope注解
@Retention(RUNTIME)
public @interface ActivityScope {
}

2.未使用@Scope注解效果展示:

我们先声明2个Activity及相关代码:
两个小明的Activity:

public class A03Activity extends AppCompatActivity {

    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_student1)
    TextView tvStudent1;
    @BindView(R.id.tv_student2)
    TextView tvStudent2;

    @Inject
    Student student1;
    @Inject
    Student student2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a03);
        ButterKnife.bind(this);
        DaggerA03Component.builder()
                .a03Module(new A03Module(this))
                .build()
                .inject(this);

        //打印两个Student类
        tvStudent1.setText(student1.toString());
        tvStudent2.setText(student2.toString());
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        startActivity(new Intent(this, A04Activity.class));
    }
}

小刚的Activity:

public class A04Activity extends AppCompatActivity {

    @BindView(R.id.tv_student)
    TextView tvStudent;

    @Inject
    Student student;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a04);
        ButterKnife.bind(this);
        DaggerA04Component.builder()
                .a04Module(new A04Module(this))
                .build()
                .inject(this);
        //打印Student
        tvStudent.setText(student.toString());
    }
}

相关的两个Module:

@Module
public class A03Module {

    private A03Activity activity;

    public A03Module(A03Activity activity) {
        this.activity = activity;
    }

    @Provides
    Student provideStudent() {
        return new Student();
    }
}

@Module
public class A04Module {

    private A04Activity activity;

    public A04Module(A04Activity activity) {
        this.activity = activity;
    }

    @Provides
    Student provideStudent() {
        return new Student();
    }

}

相关的Component:

@Component(modules = A03Module.class)
public interface A03Component {

    void inject(A03Activity activity);

}
@Component(modules = A04Module.class)
public interface A04Component {

    void inject(A04Activity activity);

}

编译成功后运行,结果如下:


这里写图片描述

可以看到,未使用自定义@Scope注解,每次@Inject注解获取的Student对象都是一个新的对象。

这样展示在界面的效果是3个不同的Student:

ActivityA中的两个Student是小明和小红(并未实现Activity局部单例)
ActivityB中的Student是小刚

3.使用自定义@Scope注解:

下面我们稍作几行代码的改变,实现Activity局部单例:

(1).在Module中添加@ActivityScope注解:

@Module
public class A03Module {

    private A03Activity activity;

    public A03Module(A03Activity activity) {
        this.activity = activity;
    }

    @Provides
    @ActivityScope//添加注解实现局部单例
    Student provideStudent() {
        return new Student();
    }

}

(2).在Component中同样添加@ActivityScope注解,否则会编译报错:

@ActivityScope//添加注解实现局部单例
@Component(modules = A03Module.class, dependencies = AppComponent.class)
public interface A03Component {

    void inject(A03Activity activity);

}

好的,仅仅添加两行注解代码,我们接下来运行看结果:

这里写图片描述

成功!我们可以看到我们成功实现了这样的效果:

ActivityA中的两个Student都是小明(Activity局部单例)
ActivityB中的Student是小刚(换了Activity,生成另外一个对象)

也就是说,在添加@ActivityScope后,该Activity中通过@Inject依赖注入生成的Student对象全部唯一,但单例范围仅仅是该Activity中,出了该Activity,生成的Student仍然是非单例的。

三.@Singleton@Scope注解原理分析

这样实在太神奇了,我们不禁想到,仅仅通过这样一个声明,就能实现对象的生成和Activity的生命周期绑定吗?只要该Activity存在,里面的Student对象就是单例?

仅仅通过这样的声明,就能实现”同生共死“吗:

@Scope
@Retention(RUNTIME) //Activity局部单例
public @interface ActivityScope {

}

@Scope
@Retention(RUNTIME) //Fragment局部单例
public @interface FragmentScope {

}

分析源码前先公布答案:

自定义的@Singleton、@ActivityScope注解根本就没有这些功能,它的作用仅仅是”标记“。**

是不是难以置信,事实上的确如此,以小明的Activity为例,我们查看编译器为我们生成的源码目录:


这里写图片描述

我们发现,无论是否添加@ActivityScope注解,自动生成的文件目录中文件的数量都没有改变,如上图,也就是说:

自定义@Scope注解并没有生成任何文件。

那么自定义@Scope注解是如何实现单例的呢

(1)A03Activity_MembersInjector.java

我们轻车熟路(如果您仔细阅读了前几篇文章的话)打开这个文件,发现无论是否添加了@Scope注解,代码皆如下

public final class A03Activity_MembersInjector implements MembersInjector<A03Activity> {
  private final Provider<Student> student1AndStudent2Provider;

  public A03Activity_MembersInjector(Provider<Student> student1AndStudent2Provider) {
    assert student1AndStudent2Provider != null;
    this.student1AndStudent2Provider = student1AndStudent2Provider;
  }

  public static MembersInjector<A03Activity> create(Provider<Student> student1AndStudent2Provider) {
    return new A03Activity_MembersInjector(student1AndStudent2Provider);
  }

  @Override
  public void injectMembers(A03Activity instance) {
    if (instance == null) {
      throw new NullPointerException("Cannot inject members into a null reference");
    }
    //请注意该行
    instance.student1 = student1AndStudent2Provider.get();
    instance.student2 = student1AndStudent2Provider.get();
  }

  public static void injectStudent1(A03Activity instance, Provider<Student> student1Provider) {
    instance.student1 = student1Provider.get();
  }

  public static void injectStudent2(A03Activity instance, Provider<Student> student2Provider) {
    instance.student2 = student2Provider.get();
  }
}

需要注意的是,当我们依赖注入Activity时,最重要的其实是这两行代码:

  //请注意该行
  instance.student1 = student1AndStudent2Provider.get();
  instance.student2 = student1AndStudent2Provider.get();

无论是否添加了@ActivityScope注解,Activity中的俩个Student对象,都是通过student1AndStudent2Provider.get()进行实例化的,也就是说,当添加了@ActivityScope之后,get()方法会生成单例,否则就没有单例,我们来看一下student1AndStudent2Provider对象是如何实例化的:

 private final Provider<Student> student1AndStudent2Provider;

 public A03Activity_MembersInjector(Provider<Student> student1AndStudent2Provider) {
 //2.构造方法中实例化student1AndStudent2Provider
    assert student1AndStudent2Provider != null;
    this.student1AndStudent2Provider = student1AndStudent2Provider;
  }

  public static MembersInjector<A03Activity> create(Provider<Student> student1AndStudent2Provider) {
  //1.有对象执行了该方法,然后执行构造方法
    return new 
    A03Activity_MembersInjector(student1AndStudent2Provider);
  }

可以看到student1AndStudent2Provider是通过有对象执行create()方法,将student1AndStudent2Provider作为参数传进来进行的初始化,我们找到执行create()方法的地方:DaggerA03Component.java

(2)DaggerA03Component.java

通过对比DaggerA03Component.java,在Provider初始化的地方,我们发现了端倪:

a.未添加@ActivityScope:

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    //注意这行代码
    this.provideStudentProvider = A03Module_ProvideStudentFactory.create(builder.a03Module);

    this.a03ActivityMembersInjector = A03Activity_MembersInjector.create(provideStudentProvider);
  }

b.添加了@ActivityScope:

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
   //注意这行代码,多了一个DoubleCheck.provider()方法
    this.provideStudentProvider =
        DoubleCheck.provider(A03Module_ProvideStudentFactory.create(builder.a03Module));

    this.a03ActivityMembersInjector = A03Activity_MembersInjector.create(provideStudentProvider);
  }

我们恍然大悟: @ActivityScope注解改变的只是做一个标记,然后Component将有标记的对象工厂类(本文为Student工厂)进行了一次”DoubleCheck“单例加工!

我们有理由进行这样的猜测:

1.未添加自定义@Scope:每次Activity初始化对象,直接让工厂类初始化一个对象给Activty(activity.student = new Student());

2.添加了自定义@Scope:每次Activity初始化对象,直接让工厂类将单例对象给Activty(
activity.student1 = factory.student(单例);
activity.student2 = factory.student(单例););

是不是有一点恍然大悟的感觉?我们为了验证我们猜测的正确性,我们打开DoubleCheck类进行分析:

(3*)DoubleCheck.java

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

  private volatile Provider<T> provider;
  private volatile Object instance = UNINITIALIZED;

  private DoubleCheck(Provider<T> provider) {
    assert provider != null;
    this.provider = provider;
  }

 // 单例获取对象
  @Override
  public T get() {
    Object result = instance;
    if (result == UNINITIALIZED) {
      synchronized (this) {
        result = instance;
        if (result == UNINITIALIZED) {
          result = provider.get();
          Object currentInstance = instance;
          if (currentInstance != UNINITIALIZED && currentInstance != result) {
            throw new IllegalStateException("Scoped provider was invoked recursively returning "
                + "different results: " + currentInstance + " & " + result);
          }
          instance = result;
          provider = null;
        }
      }
    }
    return (T) result;
  }
  
  public static <T> Provider<T> provider(Provider<T> delegate) {
    checkNotNull(delegate);
    if (delegate instanceof DoubleCheck) {
      return delegate;
    }
    return new DoubleCheck<T>(delegate);
  }

  public static <T> Lazy<T> lazy(Provider<T> provider) {
    if (provider instanceof Lazy) {
      @SuppressWarnings("unchecked")
      final Lazy<T> lazy = (Lazy<T>) provider;
    }
    return new DoubleCheck<T>(checkNotNull(provider));
  }
}

其中:

public final class DoubleCheck<T> implements Provider<T>, Lazy<T>

看到这行,我们明白了,其实和普通的Provider工厂类一样,DoubleCheck也是实现了Provider<T>接口,这样通过调用get(),就能获得对象的实例了,只不过稍微有所区别的是:

通过DoubleCheck.get()获得的对象,是单例的。

至于DoubleCheck.get()是如何实现单例的(似乎和我们普通实现单例的方式所有不同),篇幅所限,就不过多介绍,有兴趣的同学看看这篇博客 多线程问题与double-check小结 @usher,相信会有所收获。

总结

看到最后,相信大家已经对于@Scope和@Singleton的原理了解的差不多了,其实并没有什么魔力,能够真正通过一个注解,实现对Application生命周期或者Activity/Fragment生命周期进行绑定,无非做出一个标记的作用,检查到标记后,Component将对应的工厂实现单例模式,这样我们的容器去获取对象时就能实现对应的单例效果。

而对于其他的Activity,因为ActivityA和ActivityB是同一层级互不干扰的,ActivityA的Component和ActivityB的Component互不联系,当然不可能相互共享单例:

这里写图片描述

借用@MarkZhai 大神的图和一句话:位于上层的component是看不到下层的,而下层则可以使用上层的,但不能引用同一层相邻component内的实例。

同时,当我们的容器(Application/Activity/Fragment等)销毁时,对应的Component当然也不复存在,这就是为什么我们看起来@Scope能够实现”同生共死“效果的原因。

至此,Dagger2相关的知识点我们已经了解的差不多了,我们需要的就是更多的去使用它,通过它实现快速开发和代码解耦。

代码已托管Github,源码传送门,点我查看

2017/8/24日更新

事实上,Android开发中使用Dagger,开发人员仍然需要面对一些问题。

google工程师们尝试弥补Dagger的问题,于是Dagger2-Android,基于Dagger2,应用于Android开发,由google开发的的拓展库应运而生:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容