How to create an object using constructor injection?

偶尔善良 提交于 2019-12-03 02:30:59
Paradiesstaub

To create an object using the Dagger 2 constructor injection feature you need to add a method to a component which provides a Cat class.

@Component(
    dependencies = ApplicationComponent.class,
    modules = CatModule.class)
public interface ActivityComponent {
    void inject(final CatActivity a);
    // objects exposed to sub-components
    Cat cat();
    Dog dog();
}

An instance of Dog can then be retrived by calling [Component].dog().

final ActivityComponent comp = DaggerActivityComponent.builder()
            .applicationComponent(app.getApplicationComponent())
            .build();

final Dog d = comp.dog();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!