Author: Xiao La Yue

Address: http://www.jianshu.com/p/dce5382fec5d

Disclaimer: This is xiao La YueOriginal, has been authorized to publish, do not reprint without the permission of the original author

preface

A random search of Dragger2 on the Internet, a lot of articles bloomed out, at the beginning of the annotation @inject, @module and so on what do, explain a lot of, after reading a face confused. For those of you who are just starting to Dagger, this article just makes you feel like you have no love for yourself, and you still don’t know how to use it after reading it?

The first thing we need to know about new knowledge is what does it do? What can be achieved? Then write a demo of your own to play with, and read the articles when you can use them easily. This will give you a big boost in confidence. As opposed to starting with a bunch of notes and notes and stuff like that, and going around and around and confusing yourself.

In view of such a criticism, I have summed up the knowledge related to Dragger for your reference.

First of all, if you want to learn dagger well, you have to understand what “dependency injection” is because Dagger2 is a framework for implementing dependency injection. Then, we’ll go through the demo, step by step code to understand how to create god like effects; Finally, after we know how to use it, we should know the principle, APT, object diagram and so on.

Of course, for most people, it’s ok to know how to use it, but if you want to improve your skills in the interview, then follow me, let’s work together

Once you have learned dagger, you will find that APT frameworks like ButterKnife all have similarities.

From this series of articles, you will not only learn the Dagger related programming knowledge, but also learn the core competence of a programmer, learning ability. Would you rather get a fish or learn how to fish? This series of articles not only give you the fish you want at the moment, solve the urgent problem; And it’ll give you fish for a long time. Why not?

With that said, let’s take a dagger tour. Hope that through this series of articles, so that you can harvest a lot.

Dependency injection

What? What is dependency injection? Now, assuming this is your blind spot, explain some dependency injection through your first reaction. Need you, need you. Injection: For you. To connect means: to give you what you need. For example, if you’re going to work, how do you get there? If you drive, you depend on the car; But you don’t have a car, so your dad bought you a Porsche. Give you the car you need to get to work. That’s injection. Linked together, this is typical dependency injection. Don’t understand? Well, let’s do it with the code.

// Define a car

Public class Car {} // Defines a person who needs a Car. public class Person { Car car; public Person(Car car) { this.car = car; }}

The constructor can inject a Car when constructing a Person because the Person comes into the Car. So this is one way of doing dependency injection, what else can we do besides inject (pass parameters) through the constructor? That’s right, set method.

public class Person {

Car car; public Person(Car car) { this.car = car; Public void setCar(Car Car){this. Car = Car; }}

The code above injects Car into the Person class via setter methods. Of course, in addition to the above methods, there are other methods, such as interfaces. You just need to understand what dependency injection means.

So what can we expect from our Dagger2? What can be achieved? Why use Dagger2? We will need to break through one problem after another. After understanding one problem, we will proceed to the next one. Otherwise, after speaking, you will be in a fog. This is also a learning habit I have adhered to for many years.

public class Person {

// dagger2 directly uses @inject @inject Car; public Person(Car car) { this.car = car; } public void setCar(Car car){ this.car = car; }}

Dagger2 gives us the effect of directly using @Inject annotation for initialization purposes. Amazing, isn’t it? When we’re done, we’ll come back to why Dagger2? What are the benefits compared to the previous injection methods? We are going to use Dagger2 as demo code, because we are all programmers and code is worth a thousand words

Recommended reading

1, Android frosted glass blur effect, I use OpenCV to do

Android DataBinding & MVVM

3. I don’t write a line of code to implement the Toolbar! And you’re still wrapping BaseActivity?