Dagger2浅析

Dagger2是由google与square公司联合开发的一款依赖注入库。

注解符号

  • @Inject 标注需要注入的对象

  • @Module 提供依赖注入的对象

  • @Provides 在Module中,负责提供注入对象的方法

  • @Component 负责将依赖注入对象提供给目标对象中,充当了桥梁的作用。



附加的注解

  • Scope 十分重要,标注了对象实例的范围

  • Qualifier 限定符,为了防止依赖对象的注入产生混淆(因为可能需要对象名相同但内容不同)

  • Singleton



Dagger2创建步骤:

Step1
          使用@Inject标注需要的依赖注入对象

Step2
          使用@Module提供所需的依赖注入对象

Step3
          使用@Component做为桥梁,负责将依赖注入对象提供到目标对象中

Step4
          创建Component实例,完成对象的依赖注入



Gradle环境配置

project级build.gradle


buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()

        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

module级build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.android.dagger2demo"
        minSdkVersion 17
        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.4.0'

    compile 'com.google.dagger:dagger:2.4'
    apt 'com.google.dagger:dagger-compiler:2.4'
    //provided 'org.glassfish:javax.annotation:10.0-b28'
}



创建步骤讲解

Model实体

package com.android.dagger2demo;

public class Student {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



Step1 使用了@Inject注解所需依赖对象

package com.android.dagger2demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import javax.inject.Inject;


public class MainActivity extends Activity {

    @Inject
    Student student;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StudentComponent component = DaggerStudentComponent.builder().studentModule(new StudentModule()).build();
        component.inject(this);

        Toast.makeText(this,student.getName(),Toast.LENGTH_SHORT).show();

    }
}



Step2 使用@Module提供所需的依赖注入对象

package com.android.dagger2demo;



import dagger.Module;
import dagger.Provides;

@Module
class StudentModule {

    @Provides
    Student provideStudent(){

        Student student = new Student();
        student.setName("wiseclown");
        return student;
    }
}



Step3 使用@Component做为桥梁,负责将依赖注入对象提供到目标对象中

package com.android.dagger2demo;



import dagger.Component;


@Component(modules = StudentModule.class)
public interface StudentComponent {

    void inject(MainActivity activity);

}



Step4 创建Component实例,完成对象的依赖注入

StudentComponent component = DaggerStudentComponent.builder().studentModule(new StudentModule()).build();
component.inject(this);

DaggerStudentComponent是有Dagger2编译生成的,如果没有,自己需要手动rebuild一下工程。



重要信息

若@Provides中的注解方法是static,则Component实例化可以使用create方式,即
StudentComponent component = DaggerStudentComponent.create( );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值