美文网首页
dagger2和hilt的使用

dagger2和hilt的使用

作者: 壹元伍角叁分 | 来源:发表于2021-11-21 16:15 被阅读0次

一、dagger2的使用

1、dagger是什么?

dagger对对象之间的解耦,降低对象间的耦合度。

2、dagger2简单使用

  1. 在app模块的build.gradle文件中进行如下配置

    dependencies {
        ...
        implementation 'com.google.dagger:dagger:2.9' // 导入api支持,包括注解
        annotationProcessor 'com.google.dagger:dagger-compiler:2.9'// 导入dagger2的注解处理器apt
        ...
    }
    
  2. 构建类

    2.1 对象bean

    public class Student {
    }
    

    2.2 module类:用来提供对象

    @Module
    public class StudentModule {
    
        @Provides
        public Student getStudent() {
            return new Student(); 
        }
    }
    

    2.3 Component类

    @Component(modules = StudentModule.class)
    public interface StudentComponent {
    
        // 注入到 MainActivity
        void injectMainActivity(MainActivity mainActivity);
    }
    
  3. 在MainActivity中使用

     @Inject
     Student student;
     @Inject
     Student student2;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // 两种注入方式
        //方式一:简写版。DaggerStudentComponent是编译生成的代码
        DaggerStudentComponent.create().injectMainActivity(this);
        
        //方式二:自己注入
        DaggerStudentComponent.builder().studentModule(new StudentModule()).build()
               // 到这里,初始化了module和component
               .injectMainActivity(this);
     
        // student和student2的值不同。那如何设置成单例呢?
        Log.e(TAG, "onCreate: student:" + student.hashCode());
        Log.e(TAG, "onCreate: student2:" + student2.hashCode());
     } 
    

3、dagger2单例的使用

  1. 局部单例(单个activity中单例):只需在module类和Component类增加@Singleton注解。那student和student2的值就相同了

    1.1 module类:用来提供对象

    @Singleton
    @Module
    public class StudentModule {
    
        @Singleton
        @Provides
        public Student getStudent() {
            return new Student(); 
        }
    }
    

    1.2 Component类

    @Singleton
    @Component(modules = StudentModule.class)
    public interface StudentComponent {
    
        // 注入到 MainActivity
        void injectMainActivity(MainActivity mainActivity);
    }
    
  2. 全局单例

    2.1 创建一个application,在onCreate中进行初始化

    public class SingleApplication extends Application {
        StudentComponent build;
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            build = DaggerStudentComponent.builder().studentModule(new StudentModule()).build();
        }
    
        public StudentComponent getBuild() {
            return build;
        }
    }
    

    2.2 activity中直接进行调用

    ((SingleApplication) (getApplication())).getBuild().injectMainActivity(this);
    
    ((SingleApplication) (getApplication())).getBuild().injectMainActivity2(this);
    

二、hilt的简单使用

1、hilt是什么?

hilt是对dagger2的二次封装, 使用了apt+字节码插装,省略了component注入的细节,让用户用起来更加简单。

Hilt 目前支持以下 Android 类: Application、Activity、Fragment、Service、BroadcastReceiver、View

2、相比dagger2所做的优化:

  1. 无需编写大量的Component代码
  2. Scope也会与Component自动绑定
  3. 预定义绑定,例如 Application与Activity
  4. 预定义的限定符,例如@ApplicationContext与@ActivityContext

3、hilt简单使用

  1. 在项目模块的build.gradle文件中进行如下配置

    buildscript {
        dependencies {
            ...
            classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
            ...
        }
    }
    
  2. 在app模块的build.gradle文件中进行如下配置

    apply plugin: 'dagger.hilt.android.plugin' // 导入gradle-plugin 字节码插庄
    
    dependencies {
        ...
        implementation "com.google.dagger:hilt-android:2.28-alpha" // hilt的支持 百分之八十的代码是来自Dagger2
        annotationProcessor "com.google.dagger:hilt-android-compiler:2.28-alpha" // Dagger的注解处理器APT 依赖使用
        ...
    }
    
  3. 构建类

    2.1 对象bean

    public class Student {
    }
    

    2.2 module类:用来提供对象

    // ActivityComponent.class    能注入到Activity,不能注入到Application
    // ApplicationComponent.class 能注入到Activity, 能注入到Application
    @Module
    @InstallIn(ActivityComponent.class)// 注入到Activity里面去
    public class StudentModule {
    
        @Provides
        public Student getStudent() {
            return new Student();
        }
    }
    

    2.3 application类

    @HiltAndroidApp // 通过字节码插装,将我们的application继承Hilt_Application,帮我们完成注入功能
    public class MyHiltApplication extends Application {
    }
    
  4. 在MainActivity中使用

    @AndroidEntryPoint // 通过字节码插装,将我们MainActivity继承Hilt_MainActivity,在onCreate方法中帮我们完成注入功能
    public class MainActivity extends AppCompatActivity {
    
        private final String TAG = CommonMainActivity.class.getSimpleName();
    
        @Inject
        Student student;
        @Inject
        Student student2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // hilt这里无需进行注入。
            Log.e(TAG, "onCreate: student:" + student.hashCode());
            Log.e(TAG, "onCreate: student2:" + student2.hashCode());
        }
    }
    

4、hilt单例的使用

  1. 局部单例(单个activity中单例):只需在module类的方法上增加@ActivityScoped注解。那student和student2的值就相同了

    1.1 module类:用来提供对象

    @Module
    @InstallIn(ActivityComponent.class)// 注入到Activity里面去
    public class StudentModule {
    
        @Provides 
        @ActivityScoped //@ActivityScoped和上面的InstallIn(ActivityComponent.class) 配合使用,才能局部单例
        public Student getStudent() {
            return new Student();
        }
    }
    
  2. 全局单例

    2.1 module类

    @Module
    @InstallIn(ApplicationComponent.class)// 注入到Activity里面去
    public class StudentModule {
    
        @Provides
        @Singleton // @Singleton和上面的InstallIn(ApplicationComponent.class) 配合使用,才能全局单例
        public Student getStudent() {
            return new Student();
        }
    }
    

相关文章

网友评论

      本文标题:dagger2和hilt的使用

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