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( );