美文网首页
Dagger2的基本使用

Dagger2的基本使用

作者: notrynobug | 来源:发表于2017-02-14 16:43 被阅读0次

    关于Dagger2的好处我就不多说,直接上代码,直接讲解使用过程,简单粗暴。

    导入Dagger2

    使用Dagger2之前需要一些配置,该配置是在Android Studio中进行操作。
    在工程的build.gradle文件中添加android-apt插件(该插件后面介绍)

    buildscript {
        ....
        dependencies {
            classpath 'com.android.tools.build:gradle:2.1.0'
            // 添加android-apt 插件
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        }
    }
    

    在app的中的build.gradle文件中添加配置

    apply plugin: 'com.android.application'
    // 应用插件
    apply plugin: 'com.neenbedankt.android-apt'
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
        defaultConfig {
            applicationId "com.mahao.alex.architecture"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.3.0'
        // dagger 2 的配置
        compile 'com.google.dagger:dagger:2.4'
        apt 'com.google.dagger:dagger-compiler:2.4'
        compile 'org.glassfish:javax.annotation:10.0-b28'// 添加java 注解库
    }
    

    以上两个配置就可以了。

    Dagger2标识解释

    Module Compenent Container三者之间的关系


    Module 提供依赖
    Container 使用者可以使Activity Fragment
    Compenent 连接Module 和 Container


    • @Inject:
      通常在需要依赖的地方使用这个注解。换句话说,你用它告诉Dagger这个类或者字段需要依赖注入。这样,Dagger就会构造一个这个类的实例并满足他们的依赖。
    • @Module:
      Modules类里面的方法专门提供依赖,所以我们定义一个类,用@Module注解,这样Dagger在构造类的实例时候,就知道从哪里去找到需要的依赖。
    • @Provide:
      在Modules中,我们定义的方法用这个注解,以此来告诉Dagger我们想要构造对象并提供这些依赖。
    • @Component
      Components从根本上来说就是一个注入器,也可以说是@Inject和@Module的桥梁,它的主要作用就是连接这两个部分。Components可以提供所有定义了的类型的实例。

    代码部分

    说了这些概念可能会有点抽象,那么结合代码一定能方便理解
    大致功能很简单,创建一个带数据Bean,通过这个Bean给TextView显示出来

    //Bean
    public class DemoBean {
        String name;
        public DemoBean(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    
    //这是module类
    @Module
    public class AppdataModule {
        private String string1;
        public AppdataModule(String string1) {
            this.string1 = string1;
        }
        @Provides
        public DemoBean provides(String string) {
            return new DemoBean(string);
        }
        @Provides
        public String getstring() {
            return string1;
        }
    }
    

    看到上面module,很多人会问为什么在返回DemoBean对象的时候不直接添加AppdataModule 构造方法得来的string1?效果如下

        public DemoBean provides() {
            return new DemoBean(string1);
        }```
    当然是可以,但是如果我们在写了其他方法,其他方法也需要用到String呢?所以我们需要写getstring方法去返回String。
    还有人会问getstring返回的值怎么就会传到provides方法里面呢?这就涉及到Dagger内部的机制,就拿provides方法为例,provides方法需要传入一个String,那么Dagger会在其Module里去寻找返回值为String的方法,并将返回值拿来使用。
    

    //Component的代码
    @Component(modules = AppdataModule.class)
    public interface AppdataComponent {
    void inject(MainActivity mainActivity);
    }

    我们可以看到它将Container也就是MainActivity  和Module 联系起来。 这样一来Dagger就完成了。这个时候我们需要Rebuild Project,每次修改Module 之后都需要Rebuild Project
    
    ![](https://img.haomeiwen.com/i3986342/8024050af4a26631.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    

    //Dagger的使用
    @Inject
    DemoBean demoBean;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //对Dagger声明,一定要使用依赖的参数之前声明,不然会出现空指针
    DaggerAppdataComponent.builder().appdataModule(new AppdataModule("Dagger2_demo_text")).build().inject(this);
    textView = (TextView) findViewById(R.id.tv);
    //这样就完成了对Dagger注释式框架的使用
    textView.setText(demoBean.getName());
    }

    我们需要对DemoBean 进行依赖,所以要加上 @Inject,这样一来我们就不需要New了。DaggerAppdataComponent这个对象是我们Rebuild Project之后Dagger自动为我们生成的。

    相关文章

      网友评论

          本文标题:Dagger2的基本使用

          本文链接:https://www.haomeiwen.com/subject/vewxwttx.html