• The ultimate goal of this blog series is to build the MVP + Dagger2 framework
  • This blog series contains the following posts:
  1. Dagger series 1 — Prelude: Basic introduction to dependency injection
  2. Dagger 2 series (2) — Basic: @Inject, @Component
  3. Dagger: @Module & @Provides
  4. Dagger 2 series (4) — Foundation: @named & @Qualifier
  5. Dagger: @Scope & @singleton

What you will see in this article:

  1. @InjectWhat is the
  2. @ComponentWhat is the
  3. @Injectand@ComponentHow to work together.

1. The premise

Before introducing them, let’s look at a similar piece of code we saw in the project:

public class ClassA{ ClassB classB = new ClassB(); ClassC classC = new ClassC("xxx",xxx); . }Copy the code

As you can see, when we initialize other classes in the target class, this work is almost repetitive. Can we replace this work with a solution where the Dagger2 members @Inject and @Component we use pop up and shout: I can

2. What is @inject?

To implement the above functions, we need to complete the following steps:

  1. Need to annotate@Inject (Annotation)To annotate instance objects of dependent classes in the target class
  2. Also annotate@Inject (Annotation)To annotate other classes that depend onThe constructor.

The target class ClassA

public class ClassA{ @Inject ClassB classB; // Annotate dependent class instance objects with @inject ClassC ClassC; }Copy the code

Depend on the kind of ClassB

Public class class b {@inject // Use @inject to annotate the dependent class constructor publicClassB() {}... }Copy the code

Rely on class ClassC

public class ClassC{ @Inject public ClassC(XXX xxx, XXX xxx){ ... }}Copy the code

Note: When annotating class constructors with @Inject, only one constructor can be annotated, not multiple constructors.

Although both the target class and the dependent class are marked with @Inject, there is no real connection between them, so @Component acts as matchmaker.

3. What is @Component

Function: Automatically inject dependent objects generated in the Module into the Container where the dependent instance is needed

It is obvious that @Component is an annotation, so it stands to reason that a class marked by @Component is an annotation class, and that class must be an interface or abstract class. So here’s how the matchmaker works:

1.@Component Annotation class instances must exist in the target class. 2.Com Annotation will find the attributes annotated with @Inject annotation in the target class. 3. When the corresponding attribute is found, it will then find the corresponding constructor annotated with @inject. 4. All that remains is to initialize an instance of the property and assign it to it as shown below:

4. Easy to use

  • Dependency class — User, used@InjectAnnotation constructor
public class User {
    private String mName = "jack chen";
    private int mAge = 40;
    @Inject
    public User() { }

    public String getName() {
        return mName;
    }
    
    public void setName(String mName) {
        this.mName = mName;
    }

    public int getAge() {
        return mAge;
    }

    public void setAge(int mAge) { this.mAge = mAge; }}Copy the code
  • Media class – UserComponentm (interface), used@ComponentNote that class
@Component
public interface UserComponent {
    void inject(MainActivity mMainActivity);
}
Copy the code
  • The target class, MainActivity, is used@InjectAnnotate the relevant instance variables
public class MainActivity extends AppCompatActivity {

    @Inject
    User mUser;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); / / this code is the key to complete the dependency injection DaggerUserComponent. Builder (). The build () inject (this); Log.e(TAG,"name : " + mUser.getName() + " age :" + mUser.getAge());
        mUser.setAge(50);
        mUser.setName("blur");
        Log.e(TAG,"name2 : " + mUser.getName() + " age2 :"+ mUser.getAge()); }}Copy the code
  • The execution result
 name : jack chen age :40
 name2 : blur age2 :50
Copy the code

5. To summarize

So far, we have implemented basic Dagger2-based dependency injection. Let’s go through the process again:

  1. with@InjectAnnotations annotate instance objects of dependent classes in the target class
  2. with@InjectAnnotations annotate the constructors of dependent classes
  3. Repeat the above two steps if other classes depend on other classes
  4. callComponentOf the injectorinjectXXXThe (Object) method starts injecting (injectXXXMethod name is the officially recommended name, starting with Inject)

Component acts as a medium between the target class and its own dependent classes, injecting instances of the target class dependencies into the target class to initialize dependent instance variables in the target class.

5. Refer to the article


Dagger,

This is Dagger2

Dagger2 Introductory Practice

Android: Dagger2 will make you love it – The end

The simplest introduction to Dagger2

Android: Dagger2 will keep you hooked – The foundation dependency Injection Framework