Skip to content

Instantly share code, notes, and snippets.

@guliash
Created October 11, 2017 13:17
Show Gist options
  • Save guliash/f51b90a7e21e670d92dbb31e51963d1e to your computer and use it in GitHub Desktop.
Save guliash/f51b90a7e21e670d92dbb31e51963d1e to your computer and use it in GitHub Desktop.
Dagger2 MembersInjector example
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