Dagger2 explore 2-- four basic components (ii) record

       Book connected to Wen, let's review the contents of the previous chapter written.

       SUMMARY probably is marked with an injected @Inject the Activity class, and also make a mark on @Inject constructor of the class, then @Component connected to both sides of creating and injecting the object. Overall process plain and boring.

       Here we may have found a peanut watermelon!

       If you want to inject a third-party library it? I can not go and packaged third-party libraries open, then give it a constructor to make a @Inject!

       So we're going to introduce the two components of the four basic components: @Module and @Provide.

       Both are supporting the use of, I change it directly in the last chapter:

       FirstModule first create a new class:

@Module
public class FirstModule {

    @Provides
    Tools provideTools(){
        return new Tools();
    }
}

       @Provide marking method is to provide a method dependent, the general method name written provide + class name.

       Convention, convention! Long live the understanding, Li Jie Hooray!

       Then FirstComponent inside with dependent

@Component(modules = FirstModule.class)//绑定Module
public interface FirstComponent {

    void inject(FirstActivity activity);

}

       Why is not the last chapter I strongly recommend the use of create () instead of the builder (). Build () do?

       The reason here, if you add a Module can not use the create (), and must be changed back.

       Activity in the injection statement also changes:

DaggerFirstComponent.builder()
                .firstModule(new FirstModule())//添加Module
                .build()
                .inject(this);

      If the added code red mark, to prove that you do not compile, DaggerFirstComponent generated without this firstModule () method.

      The above code is very simple, to be the original mark in the mark in the class constructor, moved @Module years, @ Module is like a box, repeat this behind us.

      Start and look to see what generates AS gave us interesting things.

      Old rules, according to the logic, look DaggerFirstComponent.java

 1 public final class DaggerFirstComponent implements FirstComponent {
 2   private Provider<Tools> provideToolsProvider;
 3 
 4   private MembersInjector<FirstActivity> firstActivityMembersInjector;
 5 
 6   private DaggerFirstComponent(Builder builder) {
 7     assert builder != null;
 8     initialize(builder);
 9   }
10 
11   public static Builder builder() {
12     return new Builder();
13   }
14 
15   public static FirstComponent create() {
16     return builder().build();
17   }
18 
19   @SuppressWarnings("unchecked")
20   private void initialize(final Builder builder) {
21 
22     this.provideToolsProvider = FirstModule_ProvideToolsFactory.create(builder.firstModule);
23 
24     this.firstActivityMembersInjector = FirstActivity_MembersInjector.create(provideToolsProvider);
25   }
26 
27   @Override
28   public void inject(FirstActivity activity) {
29     firstActivityMembersInjector.injectMembers(activity);
30   }
31 
32   public static final class Builder {
33     private FirstModule firstModule;
34 
35     private Builder() {}
36 
37     public FirstComponent build() {
38       if (firstModule == null) {
39         this.firstModule = new FirstModule();
40       }
41       return new DaggerFirstComponent(this);
42     }
43 
44     public Builder firstModule(FirstModule firstModule) {
45       this.firstModule = Preconditions.checkNotNull(firstModule);
46       return this;
47     }
48   }
49 }

      Long, do not look, there is a need to come back a little later to turn.

      First builder (), the builder pattern, nothing changes. Builder but more a class method, i.e. the aforementioned firstModule ():

    public Builder firstModule(FirstModule firstModule) {
      this.firstModule = Preconditions.checkNotNull(firstModule);
      return this;
    }

      In fact, not doing, it saved a Module instances in the Builder, we provide Module in just a method that returns a new Tools ().

      Here is a checkNotNull, well understood, I do not say.

      Then build (), but also on DaggerFirstComponent initialized. (Why do I have to say they?)

      It appears to be the same old tune, but we really did not find it inside the same thing, look at the code in the initialize method

this.provideToolsProvider = FirstModule_ProvideToolsFactory.create(builder.firstModule);

this.firstActivityMembersInjector = FirstActivity_MembersInjector.create(provideToolsProvider);

      The factory class and not the same as the previous, FirstModule_ProvideToolsFactory.

      See a very familiar word: Provide. Yes, this new facility is a new class factory class @Module and @Provide combination formed.

      Look at this new facility category code:

public final class FirstModule_ProvideToolsFactory implements Factory<Tools> {
  private final FirstModule module;

  public FirstModule_ProvideToolsFactory(FirstModule module) {
    assert module != null;
    this.module = module;
  }

  @Override
  public Tools get() {
    return Preconditions.checkNotNull(
        module.provideTools(), "Cannot return null from a non-@Nullable @Provides method");
  }

  public static Factory<Tools> create(FirstModule module) {
    return new FirstModule_ProvideToolsFactory(module);
  }
}

       Disappointed, not the code name long.

       create () method pass in a module instance, then the instance preserved. When needed get (), calls module.providTools ().

       The factory class and factory class only difference is that before the create () method to pass a multi-module example, to provide a method invocation.

       Around the circle, and it coincides with the method of the previous chapter.

       I believe a serious look at the first chapter of a small partner knows what will happen later.

       And so I will, I go to the previous chapter point text to copy.

       ......

       5, call DaggerFirstComponent method instantiates instances of plant and Activity class are to MemberInjecter

       6, MemberInjecter assembled, dependency successfully injected.

      It is not very familiar? In fact, there is no more difficult.

      After reading introduce these four components, I believe you have a basic understanding of Dagger2.

      Let us visualize describe dependency injection process.

      Give Activity dependency injection, the most important thing is to specify a new object is assigned to the Activity Statement by @Inject mark.

      In fact, just like Taobao to buy things, like, you declare an Activity is in order here need an object, please give me the courier gets here. (So ​​the question is, do you object?)

      Where the object to it?

      Factory class.

      As is @Provide @Inject factory or factories? We can, will do fried rice on the line.

      The only difference is, @ provide issued to the factory to have a courier delivery box @Module filled.

      I see a lot of blog love in @Module given member variables FirstActivity, calling them to write the address on the express box.

      Can it? can.

      There is no need to do? Look at the situation, personal point of view, do not call, there is no need.

      Because @Component the couriers know the address, you write do not write, it is not important. If you want to send two courier, you would not have to write two addresses?

      But here we pay attention to pit in front of us regard the @Module and @Component bound, a @Component can bind multiple @Module, but the method provided in multiple @Module not be the same (in fact, can require little skill ). But this is not to say that I pit.

      If @Inject and @Provide exist, there is always a priority ... right? (I have not read through!)

      Watson, who in front of me delete the Tools category in the @Inject yet? Who higher priority is self-evident.

      Process is like, @ Component will first go to binding @Module and @provide, not only to find @Inject.

      If there is @Provide () method @Module the bound, but you did not call firstModule () method when the injection being given.

 

      The basic source code analysis to this than we all have a basic understanding of Dagger2, here I will say something about the use of slightly more complex.

 

 

 

 

 

 

 

      Or the next chapter of it ........

Guess you like

Origin www.cnblogs.com/yuanxixing/p/11531040.html