I asked (and answered) the same question for Dagger 1 here. How would I do something similar for Dagger 2, now that ObjectGraph.inject
no longer exists.
This question could be generalized to:
How do you do members injection if the object must be created by a different framework? (in this case, a Servlet container).
To inject an object in the activity, you'd use the appComponent defined in your Application class and call the inject() method, passing in an instance of the activity that requests injection. When using activities, inject Dagger in the activity's onCreate() method before calling super.
Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.
In order to use dependency injection with the help of dagger 2 libraries, we need to add it's dependency. Go to Gradle Scripts > build. gradle(Module: app) and add the following dependencies. After adding these dependencies you need to click on Sync Now.
I've been trying to answer this same question. I think I've gotten close to how it "should" work ideally, but I'm just derping around the GitHub project and trying to figure it out based upon scraps of information there, as a lot of the documentation for Dagger 2 is still being written (as of this week).
My example code below is actually for an Android Activity, but I believe this same approach should work for the servlet you're asking about.
Add a MembersInjector<...> to your @Component interface; for example, in the below component I've added one for my MainActivity class:
package ...; import javax.inject.Singleton; import dagger.Component; import dagger.MembersInjector; @Singleton @Component(modules = { PlaygroundModule.class }) public interface MainComponent { Wizard createWizard(); MembersInjector<MainActivity> mainActivityInjector(); }
And then in your class that you want to be member-injected, at an appropriate place after object creation but before you need to use your injected members, you need to create the component and use the member injection:
package ...; // other imports import javax.inject.Inject; import dagger.MembersInjector; public class MainActivity extends ActionBarActivity { @Inject Wizard wizard; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainComponent c = DaggerMainComponent.create(); c.mainActivityInjector().injectMembers(this); // other code... } }
The one thing I'm not clear on is whether this pattern of creating the component inside the object that's supposed to be injected into is correct. It doesn't quite feel right, but, it's still very flexible since you're only binding tightly to the component and not the modules. So perhaps this is the correct approach, but perhaps it's a bit off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With