java - Dagger 2 cycle injection -


i have 2 singletone classes i'd inject them fragments, activites, ect., i've inject them each other well. , @ point allways error.

public class adverticumchecker implements iadverticumchecker {      @inject bannermanager bannermanager;      public adverticumchecker(context context) {         indexapplication.getapplication().getappcomponent().inject(this);     } }  public class bannermanager {     @inject iadverticumchecker adverticumchecker;      public bannermanager(){         indexapplication.getapplication().getappcomponent().inject(this);     } } 

these modules

@module public class bannermanagermodule {          @singleton         @provides         bannermanager providebannermanager(){             return new bannermanager();         }     }  @module public class adverticumcheckermodule {     private context context;      public adverticumcheckermodule(context context){         this.context = context;     }      @singleton     @provides     iadverticumchecker provideadverticumchecker(){         return new adverticumchecker(context);     } } 

i build graph once @ application class. , here error:

fatal exception: main java.lang.stackoverflowerror @ com.aff.index.adverticum.adverticumchecker_membersinjector.injectmembers(adverticumchecker_membersinjector.java:38) @ com.aff.index.adverticum.adverticumchecker_membersinjector.injectmembers(adverticumchecker_membersinjector.java:8) @ com.aff.index.dagger.daggerappcomponent.inject(daggerappcomponent.java:679) @ com.aff.index.adverticum.adverticumchecker.(adverticumchecker.java:50) @ com.aff.index.dagger.adverticumcheckermodule.provideadverticumchecker(adverticumcheckermodule.java:30) @ com.aff.index.dagger.adverticumcheckermodule_provideadverticumcheckerfactory.get(adverticumcheckermodule_provideadverticumcheckerfactory.java:24) @ com.aff.index.dagger.adverticumcheckermodule_provideadverticumcheckerfactory.get(adverticumcheckermodule_provideadverticumcheckerfactory.java:8) @ dagger.internal.doublecheck.get(doublecheck.java:46) @ com.aff.index.adverticum.bannermanager_membersinjector.injectmembers(bannermanager_membersinjector.java:67) @ com.aff.index.adverticum.bannermanager_membersinjector.injectmembers(bannermanager_membersinjector.java:11) @ com.aff.index.dagger.daggerappcomponent.inject(daggerappcomponent.java:704) @ com.aff.index.adverticum.bannermanager.(bannermanager.java:89) @ com.aff.index.dagger.bannermanagermodule.providebannermanager(bannermanagermodule.java:21) @ com.aff.index.dagger.bannermanagermodule_providebannermanagerfactory.get(bannermanagermodule_providebannermanagerfactory.java:24) @ com.aff.index.dagger.bannermanagermodule_providebannermanagerfactory.get(bannermanagermodule_providebannermanagerfactory.java:8) @ dagger.internal.doublecheck.get(doublecheck.java:46) @ com.aff.index.adverticum.adverticumchecker_membersinjector.injectmembers(adverticumchecker_membersinjector.java:39) @ com.aff.index.adverticum.adverticumchecker_membersinjector.injectmembers(adverticumchecker_membersinjector.java:8) @ com.aff.index.dagger.daggerappcomponent.inject(daggerappcomponent.java:679)

first things first, this...

@singleton @provides iadverticumchecker provideadverticumchecker(){     return new adverticumchecker(context); } ... public adverticumchecker(context context) {     indexapplication.getapplication().getappcomponent().inject(this); } 

circumvents di stands for, inject dependencies, rather letting static accessor in constructor inject them magically - way advertiumchecker impossible test! list dependencies constructor arguments instead , this:

@singleton @provides iadverticumchecker provideadverticumchecker(bannermanager bannermanager){     return new adverticumchecker(bannermanager); } ... private bannermanager mbannermanager; public adverticumchecker(bannermanager bannermanager) {     mbannermanager = bannermanager; } 

dagger automatically fill gaps here , construct proper graph.

secondly, in case construction of bannermanager dependent on instance of iadvertiumchecker, have cyclic dependencies dagger error out about. smells bad design, not doable differently, in case need @ runtime.

in these cases, work lazy injections, i.e. either

@inject lazy<bannermanager> mlazybannermanager; ... mlazybannermanager.get().dosomething(); 

or

iadvertiumchecker provideradvertiumchecker(lazy<bannermanager> lazybannermanager) {     return new adverticumchecker(lazybannermanager); } 

hope helps.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -