前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dagger2系列——初识

Dagger2系列——初识

作者头像
蜻蜓队长
发布2018-09-29 11:53:01
4720
发布2018-09-29 11:53:01
举报
文章被收录于专栏:Android机动车Android机动车

经过一段时间的纠结和水深火热,终于渐渐领悟了Dagger2,在此分享一下学习心得,希望同样对Dagger2水深火热的你们有点帮助。

接下来会分享一系列Dagger2内容。

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

- @Component :用于注解一个interface, 比如:
代码语言:javascript
复制
@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类提供实例化对象的类,比如:
代码语言:javascript
复制
@Module
public class AppModule {
    private Context context;

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

    @Provides//注意需要加上@Provides
    public Context getContext() {
        return context;
    }
}
代码语言:javascript
复制
@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,如下
  • 用于标注需要被实例化的对象
  • 提供实例化对象的构造函数
  • 当类被实例化对象之后,需要马上执行的方法
代码语言:javascript
复制
public class A {
    @Inject
    B b;//需要被实例化的对象
}
代码语言:javascript
复制
public class B {
    @Inject//提供对象的实例化构造函数
    public B() { 
    }
    @Inject//当构造函数被执行之后,立马执行改方法
    public void setPresenter(){
      xxx;
    }
}
-最关键的是执行编译之后

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

代码语言:javascript
复制
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;
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Android机动车 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Dagger2中常用的注解名词以及含义
    • - @Component :用于注解一个interface, 比如:
      • -@Module:给添加了@Component注解的interface类提供实例化对象的类,比如:
        • - @Provides:在标有@Module注解类的内部方法上,提供对象实例。
          • - @Singleton:单例-Dagger2帮我们实现的一个@Scope作用域。
            • -@Inject:需要用@Inject注解的地方主要有3,如下
              • -最关键的是执行编译之后
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档