dagger 2 - How to create an object using constructor injection? -


how create instance of dog component provides cat.

public final class dog {     private final cat mcat;     public final static string tag = "dog";      @inject public dog(cat cat) {         mcat = cat;         log.e(tag, "dog class created");     } } 

after experimenting dagger 2 while have no idea how use constructor injection – hint nice, thanks.

edit:
whats wrong question? after using dagger 2, following several tutorials , reading official documentation have no clue how use constructor injection feature, that's why ask here. instead of injecting cat dependency dog @inject write dogmodule providing dog object, dog regular java class. field injection works great (there lot of examples showing how use it) need use constructor injection?

to create object using dagger 2 constructor injection feature need add method component provides cat class.

@component(     dependencies = applicationcomponent.class,     modules = catmodule.class) public interface activitycomponent {     void inject(final catactivity a);     // objects exposed sub-components     cat cat();     dog dog(); } 

an instance of dog can retrived calling [component].dog().

final activitycomponent comp = daggeractivitycomponent.builder()             .applicationcomponent(app.getapplicationcomponent())             .build();  final dog d = comp.dog(); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -