Android Dagger 2 suggest change

Constructor Injection

Classes without dependencies can easily be created by dagger.

public class Engine {

    @Inject // <-- Annotate your constructor.
    public Engine() {
    }
}

This class can be provided by any component. It has no dependencies itself and is not scoped. There is no further code necessary.


Dependencies are declared as parameters in the constructor. Dagger will call the constructor and supply the dependencies, as long as those dependencies can be provided.

public class Car {

    private Engine engine;

    @Inject
    public Car(Engine engine) {
        this.engine = engine;
    }
}

This class can be provided by every component iff this component can also provide all of its dependencies—Engine in this case. Since Engine can also be constructor injected, any component can provide a Car.

You can use constructor injection whenever all of the dependencies can be provided by the component. A component can provide a dependency, if

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Table Of Contents
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close