Dagger2 practical Tutorial: getting started with Component, dagger2component
When you read this article, I assume that you already know the benefits of using Dagger. Let's get started. This is about Dagger2.HelloWorld. I hope that you can understand the most basic usage of dagger2.
1. Add dependency
compile 'com.google.dagger:dagger:2.14.1'annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
The first dependency contains the class to be used, and the second dependency is used to compile and generate code.
2. CreateComponent
Write an emptyComponent, thisComponent can be an interface or abstract class, which must be used@ Component annotation.
@Componentpublic abstract class HelloComponent {}
3. GenerateDaggerComponent
Rebuild project, goFind the corresponding app/build/source/apt/debug/yourpackageDagger * Component class, for exampleHelloComponent, after rebuild, getDaggerHelloComponent class:
public final class DaggerHelloComponent extends HelloComponent { private DaggerHelloComponent(Builder builder) {} public static Builder builder() { return new Builder(); } public static HelloComponent create() { return new Builder().build(); } public static final class Builder { private Builder() {} public HelloComponent build() { return new DaggerHelloComponent(this); } }}
Dagger is based on@ Component: the annotation processor is used to generate an implementation class for this class. This class uses "Dagger" and@ Component the Class Name of the annotation class is named in combination with the camper, which is@ Component: Implementation class of the annotation class. It has built-in ConstructorBuilder.
PassDaggerHelloComponent. create () to obtainHelloComponent typeDaggerHelloComponent instance.
PassDaggerHelloComponent. builder () can obtainDaggerHelloComponent. Builder instance, and then throughBuild () method, that isDaggerHelloComponent. builder (). build () is also availableHelloComponent typeDaggerHelloComponent instance.
Currently, the two methods for obtaining an AppComponent instance are equivalent. What are the differences between them? We will leave it for later.
CurrentlyHelloComponent instance, but obviously we can't do anything with it.
4. AddComponent Method
According to the official APIComponent must contain at least one AbstractComponent Method(Otherwise, just like above,Component is useless ). The so-calledThe Component method refersProvision MethodOrMembers-Injection Method.The Provision method is a non-parametric method that returnsInjected orProvided type. Now we only need to know@ Inject comments a non-argument constructor of a specific class to getThe injected type is enough. For exampleUser。
OneInjected type:User
public class User { private String mName; @Inject public User() { mName = "JingKe"; } public String getName() { return mName; } public void setName(String name) { mName = name; } public void say(){ Log.e("User","Hello, I'm"+mName+", I am using a Dagger"); }}
InAdd a response in HelloComponentThe abstract Provision method of the injected type:
@Componentpublic abstract class HelloComponent { public abstract User getUser();}
Rebuild project, inIn the app/build/source/apt/debug/yourpackage directory, Dagger generatesUser FactoryUser_Factory:
public final class User_Factory implements Factory { private static final User_Factory INSTANCE = new User_Factory(); @Override public User get() { return new User(); } public static User_Factory create() { return INSTANCE; }}
NewDaggerHelloComponent:
public final class DaggerHelloComponent extends HelloComponent { private DaggerHelloComponent(Builder builder) {} public static Builder builder() { return new Builder(); } public static HelloComponent create() { return new Builder().build(); } @Override public User getUser() { return new User(); } public static final class Builder { private Builder() {} public HelloComponent build() { return new DaggerHelloComponent(this); } }}
OurUser_Factory is based on the annotation Processor@ InjectUser generated, and the newDaggerHelloComponent also overwritesAbstractProvision Method.
5. UseComponent
Use the precedingDaggerHelloComponent andThe Component method can be used in the Client.HelloComponent InjectionUser.
public class Main { public static void main(String[] args) { DaggerHelloComponent.create().getUser().say(); }}
Summary
This article describes the basic usage of Dagger2, which is summarized as follows:
1. CreateComponent
2. AddComponent method,The Component methods include:Provision Method andMembers-Injection method.The Provision method has no parameters and the returned value isInjected type orProvided type. If you add a responseInjected typeProvision method, which must be defined in advanceInjected class.
3. UseComponent。
We can see thatComponent can beClient to create an instance.