The original address: www.jianshu.com/p/01d3c014b…

Dagger2 found the advantages of Dagger2, but + MVP did not particularly understand how to use it in the project.

Another author’s article was recommended in the comments

Address: www.jianshu.com/p/65737ac39…

Take the time to read through these three articles before you modify the MVP demo

1. Java: Dependency Injection (DI)

Inject advantage: New objects Inject instance annotations to annotate other classes that depend on in the target class. The same Annotation is used to annotate the constructors of other classes that depend on. That Annotation is called Inject

Component A bridge between two classes needs to reference an instance of the target class, look for the injected-annotated property, and the corresponding constructor and assign values. Component also called Injector

Module encapsulates the simple factory pattern used by third-party libraries.

Modules adds modules to Component. Modules adds modules to multiple Modules.

The Provides Module Provides class instance creation method with the Provides annotation. After Component searches for the annotation property of the target class with the Inject annotation, Component will go to the Module to find the corresponding class instance creation method with the Provides annotation. This solves the problem of third-party libraries using Dagger2 to implement dependency injection.

Note 2

Dependency injection (DI) is lost when one class is created by Inject class and Module class. Qualifier is used to identify different instance creation methods and to mark instance properties of target class responses.

Custom Scope annotations allow you to better manage the life cycle of created class instances.

There are two ways to divide components, one global, and ultimately for activities or fragments

Singleton (Define method ApplicationComponent in Module to create global class instance management Module to ensure ApplicationComponent has only one instance) Better manage the relationship between ApplicationComponent and Module to ensure that ApplicationComponent and Module match. If ApplicationComponent and Module have different scopes, an error will be reported during compilation.

Class instances of Component share dependencies in three ways: Abstract some common Component methods into a parent class that the children inherit

Scope can better manage the organization of components, better manage the matching relationship between Components and Modules, and improve readability. For example, global classes are marked with Singleton, so that the program monkey can immediately understand that this class is a global Singleton class.

Note 3

The benefits of Dagger2 increase development efficiency, eliminating repetitive simple manual labor. Better management of class instances. The decoupling

Step 1: Find if the Module has a method to create the class. Step 2: If there is a method to create a class, check whether the method has parameters. Step 2.1: If there are parameters, initialize each parameter starting from ** Step 1** step 2.2: If there are no parameters, directly initialize the class instance and the dependency injection ends. Step 3: Step 3.1: If there are parameters, initialize each parameter from ** step 1** step 3.2: If there are no parameters, directly initialize the instance of the class and the dependency injection ends hereCopy the code

`

1. An App must have a Component (named ApplicationComponent) that manages the app’s entire global class instance 2. Multiple pages can share a Component 3. A Component does not have to correspond to one or more Modules; a Component may not include Module 4. Custom Scope annotations are best used, although they can get the project running without them, but they have many benefits.

Next, analyze the Demo of Daishen

Original address: github.com/niuxiaowei/…

First, look at the project structure

Here’s an interesting thing: App DaggerAppComponent is reporting an error, but the project has been compiled and imported, and the file is in the debug folder

This file is Generated on the top. This tag is not used before.

The Demo is easy to run with two buttons: one to retrieve user information and one to display toast

The GetUserData constructor is annotated by @Inject. There is also getUser (), which returns an instance of UserData. The name property has a value. UserData is an entity class that has only one name attribute.

The DI package is the related class used by Dagger2

The components package Bridges the two classes. ActivityComponent is tagged with both Component and scopes.PerActivity and there is a getActivity() method inside. AppComponent has two tags @Singleton for Singleton and @Component for connector. There are three methods in AppComponent: get the context, get the Toast tool, and get the Navigator. MainComponent has two tags @perActivity and @Component and inherits ActivityComponent. There are two methods inject (mainActivity) and get an instance of mainFragmentComponent. The MainFragmentComponent class has two tags, @perActivity and @SubComponent, and an Inject (mainFragment) method.

Modules encapsulates a simple factory schema used by third-party libraries. AppModule is tagged with @Module, and there are three methods provideContext, provideNavigator, and provideToastUtil that are tagged with @Provides and @Singleton. The ActivityModule is also tagged by @Module, passes an activity instance in the constructor, and returns it through the provideActivity method tagged by @provides and @perActivity. The MainModule is also tagged with the @Module and has only one provideUserData() method with the @Provides flag

The scopes package only has the PerActivity interface marked by @Scope and @Retention.

The presenter package contains only the MainPresenter class, which has a static interface IUserView and its set methods and instances. Its constructor is marked by @Inject and passed in an instance of GetUserData, which passes data to the interface through the getUser method

View package BaseActivity has a getAppComponent method, BaseFragment has no method. MainActivity implements MainFragment in a monitoring interface, through DaggerMainComponent. Builder () method to obtain the ActivityModule object and invokes the inject method, The getMainComponent method return object is provided. The mainFragment class uses @Inject Get mainPresenter, toastUtil and multiConstruct in onActivityCreated method to get MainFragmentComponent object and implement inject method. The data is set by mainPresenter’s setUserView, the data is retrieved by mainPresenter in onCreateView’s two click events, and the toast is displayed by toastUtil.

The outermost App in the package gets the AppModule object in onCreate and provides the return method. The @inject constructor Navigator in the MultiConstruct class is tagged by @Singleton. ToastUtil encapsulates the method of presenting toast.

To summarize: the COMPONENTS package of the DI package is full of injectors, including the APP injector, the Activity injector, the Main injector, and the Fragment injector. The Main injector inherits the Activity injector and provides dependency injection for MainActivity. Module is a class that Provides an instance of a class. Two instances, MainModule and ActivityModule, are annotated by the main injector. Their methods are annotated by @provides. When a class in a Module needs to create an instance, it needs to be marked with @perActivity. MainComponent inherits ActivityComponent and relies on AppComponent with the Dependencies tag. The MainFragmentComponent declares the same scope via @perActivity as the MainComponent and is included via @subComponent.

Factory mode can also reduce coupling. Dependency injection: Finds an appropriate object from the container and assigns a value to the current object. Classes annotated by @Component are injectors, and beans generate factory classes that run in apt-Debug packages. Injector new cannot be created directly but only through the creator.

Inject can only be used for non-parameter constructs, and source code can be modified.

@Module and @Provides are used for parameter constructs and JAR packages. @Module for the factory pattern class and @Provides for the instance class. Annotate the Module class in the Component class. Inject looks for the @ Inject class to assign. Call to specify the factory where the instance was created.

Getting the injector requires compiling the class that generates the dagger beginning