Dagger2 use the correct posture.
The previous article "Dagger2 This introduction will not give up" in the introduction of the Dagger2 of some of the use of light, I think it is very suitable for Dagger2
the introduction of the idiot-style explanation, and later found that some content is not careful enough, some details are not explained in detail. After referring to the following three articles, add some of the previous content.
- Android:dagger2 let you down-basic Dependency Injection Framework article
- Android:dagger2 let you down-focus on the concept of interpretation, integration of the article
- Android:dagger2 let you down-the end of the chapter
The above three articles for Dagger2
the idea of clarity, and to Dagger2
make an abstract concept, some of the content of this article is also excerpted from the above article.
Have not seen "Dagger2 This introduction will not give up" may not be conducive to reading
The content of this article:
The content of this article is also not related to Dagger2
the internal principle, but to the previous article did some knowledge to supplement, so as to better useDagger2
Dagger2
two ways to provide an injection instance
- Why
ActivityComponent
you should provide a inject
method.
Scope
Use, how to achieve a single case?
Qualifier
The role of qualifiers and their use.
To clarify the idea, Dagger2 two ways to inject an instance.
It was previously said that the way to provide injected instances is by writing Moudle
, and by providing some provideXXX()
methods, and then by Component
injecting these objects.
In fact Dagger2
, there is a way to implement object injection, this method is relatively simple. Suppose we need to inject an object of Class A, we just need to add an annotation to the Class A constructor @Inject
, and then Component
provide a method to get an instance of a directly in the line. The code is as follows:
publicclass A{ @Inject publicA(){ }}//在 XXXComponent 中A getA();
Then, in the place where we need to inject the A object (Activity)
, we Component
getA()
can get the A object using the call method.
In this case, there may be some doubt, the Class A is a simple object, if a need to pass in the constructor function parameters what to do? In fact, the principle is similar to the one in the previous article, these parameters will be obtained according to the Component
methods provided in the current or dependent Component
module
provideXXX()
way.
In Dagger2
total there are two ways to provide dependency injection. In fact, after testing, in the event of a Dagger2
@Inject
modified member variable, it will look for the provided object in the first case, and then find the Component
constructor that contains it @Inject
.
Dagger2的
the entire dependency injection process is as follows:
步骤1:查找Module中是否存在创建该类的方法。步骤2:若存在创建类方法,查看该方法是否存在参数 步骤2.1:若存在参数,则按从**步骤1**开始依次初始化每个参数 步骤2.2:若不存在参数,则直接初始化该类实例,一次依赖注入到此结束步骤3:若不存在创建类方法,则查找Inject注解的构造函数, 看构造函数是否存在参数 步骤3.1:若存在参数,则从**步骤1**开始依次初始化每个参数 步骤3.2:若不存在参数,则直接初始化该类实例,一次依赖注入到此结束
Why Activitycomponent to provide a inject method
First, after my tests, when I add annotations to some of the members that need to be injected, I DaggerActivity
@Inject
Dagger2
generate DaggerActivity_MembersInjector
something, which is the key class for injecting member variables, and it involves some more in-depth content, so let's not delve into the mechanics of the operation here. We just need to know that when we need to inject some member variables, we have to ActivityComponent
provide a method in:
void inject(DaggerActivity daggerActivity);
And this method is called at initialization time to inject successfully. As for this method inject
is the official recommended name, we can not use this, but it is inject
better to understand. It is worth noting that the parameters of this method must be accurate, such as the DaggerActivity
need to inject, we need some here DaggerActivity
, can not be written BaseActvity
or Activity
, otherwise it will inject failure. Because the internal principle is to inject this parameter, it is clear that the content that needs to be DaggerActivity
injected in BaseActivity
or Activity
does not exist.
How to use scope, how to implement a single case?
The mystery Scope
is also a bit difficult to understand, where we have used it Scope
. In our AppComponent
add a note for @Singleton
, @Singleton
is one Scope
, is said to be able to achieve a single yo ... Does this make it possible to implement a single-case pattern? I was just beginning to understand that. I didn't know this was the case until I read the article a few times.
In fact @Sinleton
, there is no ability to create a singleton, so the AppComponent
dependency injection provided in is how to implement a single case. In fact, this principle is very cheap single.
First Module
, the method of creating the instance is provided, then the AppComponent
Module
management is done, and finally Appcomponent is instantiated once in the custom Applicaiton .
This instantiation is one of the most important. Just being instantiated once, that's not a single case. It's so simple.
Perhaps some of the children's boots were not happy at the time, so since this has been achieved in a single case, then @Singltop
what is the use of this? It's not superfluous.
In fact @Singletop
, there are some functions, the first one can let you face to understand that this is a single case, @Singletop
and secondly this can better manage Modlue
and Component
the relationship between.
Dagger2
It needs Component
to be guaranteed and Module
matched, and this annotation needs to be used.
Why do you say that, the person who read the previous article will know that I have defined a ActivityScope
, my previous explanation is ActivityScope
for Activity
the corresponding life cycle (at that time do not understand, is this Scope
really so magical, declare a ActivityScope
Will allow the injected object to be consistent with the activity life cycle), it is clear that this is wrong. Why do you need this Scope
? The reason is because I have it in, and I rely on it, AppComponent
@Singletop
ActivityComponent
AppComponent
So we need to use a Scope来
match between them, otherwise it will be error during compilation. Does not mean that ActivityScope
the instance and Activity
life cycle will be consistent. And the Activity
life cycle are consistent because the ActivityComponent
Activity
instances are generated in. (The specific Dagger2
internal how to protect this, and not in-depth study).
The role of the Qualifier qualifier and its use.
This is also a very powerful note, first why you need to use such a thing, said before, in Module
the provide
method is actually based on the return value to identify. But suppose I need to pass in different construction parameters according to different requirements, how can I distinguish them? For example: One Presenter
, maybe he has two constructors, corresponding to different requirements, in this case, provide
the return value of the method is Presenter
, then need to use Qualifier
. How to use it specifically.
First, you declare an annotation with a @Qualifier
modifier, and then add it where you want to distinguish it, and here's an example code:
@Module Public class appmodule { Private FinalApp app; Public Appmodule(App app) { This. App = app; }@Provides @Singleton @ForApplicationContext Provideappcontext () {returnApp }@Provides @SingletonPrefser Provideprefser (@ForApplicationContext context) {return NewPrefser (context); }@Provides @SingletonAccountmanager Provideaccountmanager (@ForApplicationContext context) {returnAccountmanager.get (context); }
First add an annotation to the provide @ForApplicition
, and then mark it again where you need to use it Context
. This assumes that there are other places provided and where there is a Activity
Context
conflict, it Dagger2
can be found exactly as well Applicaiton
Context
.
Summarize
Here to dagger the overall use of the way to make some additions, there is no source from the point of view to understand Dagger2, the next article will be combined with Dagger2 generated code for discussion, only to understand the operating mechanism, we can use it handy.
Dagger2 use the correct posture.