Introduction with some history-Dagger 2 [Part2]

Ebin Joy
3 min readOct 9, 2019

--

If you did not read part1, Click here

Dagger-2 is a dependency injection framework. It separates the creation of an object from its consumption. By using dagger2 we can enhance testability and object decoupling feature in an application. It is providing dependency injection using the java strong annotation feature.

You need to add the dagger dependencies to your ‘build.gradle’ file under the app level dependency.

Replace ‘x’ with a new version available here, at the time of writing it is 24

Till Dagger 2, look back

Java Beans introduced as a solution for dependency injection at the initial time. It comes before the introduction of Annotation in java and later adopted in Spring for dependency injection. It is purely based on XML, so headache for developers. Also, there is a chance to bulky XML configuration files. Another drawback is to identify the errors is very difficult and also traversing through the XML tree makes the developer's life a little complicated.

Later Guice was come to screen and that time java annotation and reflection came to the main screen(Java 1.5 release came annotation and reflection). So Guice used these features for the creation of dependency classes. But still, there is a problem, it is very difficult to track an issue using the stack trace.

Then Dagger1 is developed by square, the team behind Retrofit and Picasso. They solved the app debugging headache by giving meaningful names to generated classes but issues were not fixed completely. Still, there was an issue since they used Reflection for class creation and that leads to run time app crashes (for some typo errors).

Finally, Dagger2 came with a solution and it is an enhanced and perfected version of Dagger 1 by Google developers. It does not use any reflection, classes are generated using annotation so it is happening in build time. Dagger2 was very smart and it knows how to create an instance if you give correct ingredients.

As mentioned earlier dagger2 uses annotation for class creation.

An annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated.

Following are the main annotations are used in Dagger2 for dependency injection

  • @Module and @Provides: Define classes and methods which provide dependencies
  • @Inject: request dependencies. It can be used on a constructor, a field, or a method.
  • @Component: Enable selected modules and used for performing dependency injection.
  • @Qualifier is used to distinguish between objects of the same type.
  • @Scope — A scope is nothing but an annotation. We will be creating a custom scope for the login screen.

Now you may be worrying about what's going on. In the next part, I will be explaining this with the help of a real-world example.

What’s next? Part3, to read click here

--

--

Ebin Joy
Ebin Joy

Written by Ebin Joy

Builder when the sun rises and an investor in the evening.

No responses yet