Created
October 11, 2017 13:17
-
-
Save guliash/f51b90a7e21e670d92dbb31e51963d1e to your computer and use it in GitHub Desktop.
Dagger2 MembersInjector example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
A a = new A(); | |
DaggerComponentA.create().inject(a); | |
B b = new B(); | |
a.injector.injectMembers(b); | |
System.out.println(b.string); | |
} | |
} | |
@Singleton | |
@Component(modules = MyModule.class) | |
public interface ComponentA { | |
void inject(A a); | |
} | |
@Module | |
public class MyModule { | |
@Provides | |
public String string() { | |
return "42"; | |
} | |
} | |
public class A { | |
@Inject | |
MembersInjector<B> injector; | |
} | |
public class B { | |
@Inject | |
String string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment