dagger2和hilt的使用

一、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();
        }
    }
    



作者:壹元伍角叁分
链接:https://www.jianshu.com/p/66c15d5caf22
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值