question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document method and field injection, and MembersInjector

See original GitHub issue

The user guide states:

Dagger also supports method injection, though constructor or field injection are typically preferred.

However, I can find no example or explanation for the usage of this injection style, nor have my experiments yielded any working results. Is this supported? And if so how does one use it?

Simply annotating a public setter with @Inject will cause a *_MembersInjector class to be generated for that class but that MembersInjector isn’t referenced anywhere else in the project or other generated classes so clearly it’s not “known” by the object graph.

Is method injection merely meant to provide a method of injection for something that otherwise (for whatever reason) couldn’t be injected via the constructor or via field injection? Maybe something that requires additional logic during injection? So the class with the method-injected setter must still be constructed via the object graph by, say, a Provider<T> or something.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bjdupuiscommented, Apr 22, 2016

Thanks, that’s a fantastic summary.

1reaction
netdpbcommented, Apr 21, 2016

We should add examples to the user’s guide.

In the meantime, here’s a quick summary. Let me know if this helps you for now.

If any methods or fields of a class Foo are annotated with @Inject, then you can inject a MembersInjector<Foo> and pass an instance of Foo to its injectMembers() method, which will call those methods and set those fields. (You can actually inject a MembersInjector<Foo> even if it has no @Injected fields or methods; its injectMembers() method will do nothing.)

If Foo also has a constructor that’s annotated with @Inject, then the factory for Foo (Foo_Factory) will actually use that MembersInjector<Foo> while providing instances of Foo. Try your example again where you annotate both a setter and the constructor, and you should see that the members injector is used.

That’s the Dagger implementation. Now on to the use cases.

Field or method injection is useful for classes where user code (including Dagger) is not responsible for creating instances and/or constructors must not have parameters. A big example is Android classes like Application and Activity, where the constructor must not take arguments, and the platform instantiates them.

Field or method injection can also be useful in deep class hierarchies if you want to inject a dependency into a base class but you don’t want to make all subclasses add that dependency to their constructors—although in that case, you may see that as an opportunity to refactor to use composition instead of inheritance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MembersInjector - Javadoc.io
Injects dependencies into the fields and methods on instances of type T . Ignores the presence or absence of an injectable constructor. Since:...
Read more >
Android 102 : Why does dagger generate MembersInjector for ...
Dagger generates a members-injector for classes having @Inject applied to a field or a method. Which will be used for injecting all the...
Read more >
Members injection - Dagger
Members injection methods are void methods on a component that take a parameter of a specific type, allowing Dagger to set its @Inject...
Read more >
What is the MembersInjectors in Guice? - Stack Overflow
class) , Guice creates the B instance itself and injects it according to its @Inject constructor, methods, and fields. However, in some cases, ......
Read more >
com.google.inject.spi Documentation Differences
JDiff is a Javadoc doclet which generates an HTML report of all the packages, classes, constructors, methods, and fields which have been removed, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found