美文网首页
Dagger2采坑学习记录

Dagger2采坑学习记录

作者: bill_jean | 来源:发表于2018-08-23 16:04 被阅读0次

    前几日出去面试,被问及dagger2,一无所知。现在ioc框架很火,后端的Spring,到前端的Dagger2。都是同一种思想,到处应用。现在开始学习,并且记录一些使用技巧。实现原理暂且不谈,熟能生巧嘛,先用上了再说。

    1.报错

    错误: izhonghong.com.dagger2.bean.Student cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.

    izhonghong.com.dagger2.bean.Student is injected at

    izhonghong.com.dagger2.SecondActivity.student

    izhonghong.com.dagger2.SecondActivity is injected at

    izhonghong.com.dagger2.inject.compontent.SecondCompontent.inject(secondActivity)

    显示SecondComponent注入失败,指向SecondModule 看SecondModule的代码

    @PerActivity

    @Named("12345")

    public Student getStudent3(){

    Student student =new Student();

    student.setName("小黄");

    return student;

    }

    这里提供了Student数据 并且有@Named 标签 而MainModule 中也有提供Student实例的方法,并且也标注了@Named

    @Provides

    @Named("1234")

    @PerActivity

    public  Student getStudent2(){

    Student s =new Student();

    s.setName("小蓝");

    return  s;

    }


    去掉@Named标签后build成功。

    总结:

    1.当Module中只有一个同类实例方法的时候,不能添加@Name()

    2.在一个Module中实例方法调用了@Name标签后,不能在其他module中实例化该对象

    相关文章

      网友评论

          本文标题:Dagger2采坑学习记录

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