Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inject into a Servlet with Dagger 2?

Tags:

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).

like image 443
Ben Avatar asked Apr 07 '15 16:04

Ben


People also ask

How do you inject a dagger?

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.

What is dagger 2 used for?

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.

How do you add a dagger dependency?

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.


1 Answers

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.

like image 177
Mathieu Fenniak Avatar answered Sep 19 '22 12:09

Mathieu Fenniak