Android:Dagger2系列1 初识

经过一段时间的纠结和水深火热,终于渐渐领悟了Dagger2,在此分享一下学习心得,希望同样对Dagger2水深火热的你们有点帮助。
接下来我会分享一系列Dagger2内容。
下一篇:Android:Dagger2系列2 实例解析(已更新)

Dagger2中常用的注解名词以及含义

  • @Component :用于注解一个interface, 比如:

@Singleton
@Component(modules = {AppModule.class, RetrofitModule.class})
public interface AppComponent {

    IRetrofitRequest request();

    Context getContext();
}

这里用@Component标注的AppComponent接口,提供了两个方法,一个返回的是IRetrofitRequest,一个是Context。
但是这两个对象在哪里实例化呢?
编译代码:Dagger2会自动生成一个叫DaggerAppComponent的类,该类会根据@Component(modules = {AppModule.class, RetrofitModule.class}),这里的AppModule和RetrofitModule两个类中去寻找IRetrofitRequest和Context实例化的对象。如下介绍@Module

  • @Module:给添加了@Component注解的interface类提供实例化对象的类,比如:

@Module
public class AppModule {
    private Context context;

    public AppModule(Context context) {
        this.context = context;
    }

    @Provides//注意需要加上@Provides
    public Context getContext() {
        return context;
    }
}
@Module
public class RetrofitModule {

    @Provides//提供对象,必须添加该注解
    @Singleton//单例模式,这里的IRetrofitRequest 是全局的对象,接口调用的时候需要用到该类(自定义)
    public IRetrofitRequest getService() {
        //打印拦截器
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(logging)//添加打印拦截器
                .connectTimeout(30, TimeUnit.SECONDS)//设置请求超时时间
                .retryOnConnectionFailure(true)//设置出现错误进行重新连接。
                .build();
                
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConst.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(httpClient)
                .build();
        return retrofit.create(IRetrofitRequest.class);
    }
}

这里需要注意的是提供实例化对象的方法上需要添加@Provides注解

  • @Provides:在标有@Module注解类的内部方法上,提供对象实例。

  • @Singleton:单例-Dagger2帮我们实现的一个@Scope作用域。

  • @Inject:需要用@Inject注解的地方主要有3,如下

    • 用于标注需要被实例化的对象
    • 提供实例化对象的构造函数
    • 当类被实例化对象之后,需要马上执行的方法
public class A {
    @Inject
    B b;//需要被实例化的对象
}
public class B {
    @Inject//提供对象的实例化构造函数
    public B() { 
    }
    @Inject//当构造函数被执行之后,立马执行改方法
    public void setPresenter(){
      xxx;
    }
}
  • 最关键的是执行编译之后

Dagger2会自动生成很多类文件,其中一个就是DaggerXXX,这里的XXX就是用@Component标注的接口名,比如生成了DaggerAppComponent类文件,该类文件实现了AppComponent接口,并且根据相关的@Module提供的实例进行初始化。

public class App extends Application {
    private static AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(getApplicationContext()))//AppComponent关联的AppModule类
                .retrofitModule(new RetrofitModule()) //AppComponent关联的RetrofitModule类
                .build();
    }

    public static AppComponent getComponent() {
        return appComponent;
    }
}```

下一篇:[Android:Dagger2系列2 实例解析](http://www.jianshu.com/p/7abc7938818b)(已更新)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文的分析基于dagger2的2.7版本。 谷歌开发维护的Dagger2出来有很长时间了,目前在很多开源项目上也能...
    sososeen09阅读 13,659评论 31 108
  • 十岁的时候 我深信自己与众不同 预感有一件轰轰烈烈的事情发生在未来的某一天 然后成就一个不平凡的人生 十四岁的时候...
    浅浅莫言深阅读 399评论 5 2
  • 《带上灵魂去旅行》是毕淑敏的一本散文书。毕淑敏是国家一级作家,心理学家。这本书讲述了旅途中的点点滴滴。告诉我们人的...
    噫的说阅读 235评论 0 0
  • 整理学习过的知识和项目 1.挑选自己中意的公司 聪明自信始终是加分项 心态不急不躁有行动 简历,笔试,面试 不要因...
    love2013阅读 204评论 0 0
  • 1):【在线教育】 学霸君获 5000 万美元 B 轮融资,9 月份将会推出新产品,“形式上要轻于猿辅导” 让作业...
    波_洛阅读 285评论 0 1