欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Dagger2浅析

发布时间:2025/3/21 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 23buildToolsVersion "23.0.3"defaultConfig {applicationId "com.android.dagger2demo"minSdkVersion 17targetSdkVersion 23versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles 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 {@InjectStudent student;@Overrideprotected 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 {@ProvidesStudent 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( );

总结

以上是生活随笔为你收集整理的Dagger2浅析的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。