美文网首页
Android依赖注入之Dagger2

Android依赖注入之Dagger2

作者: Heweii | 来源:发表于2019-08-23 15:46 被阅读0次

    前言

    Dagger2是目前非常火的依赖注入框架,但是很多慕名而来的学者总感觉门槛高,很复杂,主要还是网上的很多教程比较复杂,没有做到由浅到深的讲解。本篇将会一步一步的实现Dagger2。

    一.Dagger2简介

    Dagger2是一个依赖注入框架,通过注解的方式减少手写过多new对象的代码,并做到充分的解耦,维护代码更便利。不仅能减少代码量,就算构造器发生变化,使用的地方也不需要做什么变化。
    还有一点要着重提下,Dagger2不是基于反射来实现的,Dagger1的原理是反射实现,所以Dagger2的性能更好,但是灵活性会差点,没有反射就没有动态机制。

    二.Dagger2使用

    1.配置

    build.gradle中加入以下配置,最新版本以官方为准

    implementation 'com.google.dagger:dagger:2.16'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.16'
    

    2.简单使用

    首先新建一个类,并加上@Inject注解,该注解其实就是做标记

    public class Person {
        @Inject
        public Person() {
        }
        public String sayHahaha(){
            return "hahaha";
        }
    }
    

    再新建Component注入器。需要注意的是inject()方法体的参数不能重复(即只能编写一个参数为MainActivity的注入器),这里是给MainActivity编写的注入器

    @Component
    public interface MainActivityComponent {
        void inject(MainActivity activity);
    }
    

    然后build一次项目,目的是生成对应的DaggerMainActivityComponent类。

    注入器的使用如下,@Inject标记的作用就显示出来了,就是为了新建Person对象

    public class MainActivity extends AppCompatActivity {
         @Inject
        Person person;
         @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            DaggerMainActivityComponent.create().inject(this);
            person.sayHahaha();
        }
    }
    

    3.有参构造函数的使用

    对于带参构造的对象,必须使用@Module注入;并且这个模型必须有一个带参的构造方法。@Provides带参提供者也是缺一不可的

    @Module
    public class Student {
    
        private String name;
    
        public Student() {
        }
    
        public Student(String name) {
            this.name = name;
        }
    
        @Provides
        public Student buildStudent(String name){
            return new Student(name);
        }
    
        @Provides
        public String getName() {
            return name;
        }
    }
    

    在原来注入器的基础上面加上modules = Student.class

    @Component(modules = Student.class)
    public interface MainActivityComponent {
        void inject(MainActivity activity);
    }
    

    然后build项目,然后直接使用就行

    @Inject
        Student student;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LoadUtils.bindView(this);
            MainActivityComponent.builder().student(new Student("111")).build().inject(this);
            student.getName();
       }
    

    4.不同构造函数的使用

    在调用一个类的时候可能会用到不同的构造器,可以用@Named方式来进行区分

    @Module
    public class Student {
    
        private String name;
    
        public Student() {
        }
    
        public Student(String name) {
            this.name = name;
        }
    
        @Provides
        @Named("noName")
        public Student buildStudent(){
            return new Student();
        }
    
        @Provides
        @Named("hasName")
        public Student buildStudent(String name){
            return new Student(name);
        }
    
        @Provides
        public String getName() {
            return name;
        }
    }
    

    使用的时候也用相同的方式区分

    @Named("hasName")
        @Inject
        Student student;
        @Named("noName")
        @Inject
        Student student2;
    

    由于用Name的形式不够严谨,这里我们更多用到自定义注解的方式,用到@Qualifier

    @Qualifier
    @Retention(RetentionPolicy.RUNTIME)
    public @interface HasNameCons {
    }
    
    @Qualifier
    @Retention(RetentionPolicy.RUNTIME)
    public @interface NoNameCons {
    }
    

    就可以直接这么使用,不太了解自定义注解的可以参考https://www.jianshu.com/p/9d20a7391057

     @Provides
        @NoNameCons 
        public Student buildStudent(){
            return new Student();
        }
    
        @Provides
        @HasNameCons 
        public Student buildStudent(String name){
            return new Student(name);
        }
    

    结语

    Dagger2无论是中大型项目都很适用,代码简洁,方便维护。本篇没有特别贴出注入器构建的代码,有兴趣的可以自己研究下,比较简单。

    相关文章

      网友评论

          本文标题:Android依赖注入之Dagger2

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