Dagger2 Advanced - Briefly say SubComponent and Qualifier

foreword

    

        Introduction and configuration of Dagger2

        The use of Dagger2 basics

        Dagger2 Advanced - Compiling and Generating Source Code Interpretation

        Dagger2 Advanced - Scope Control (Scope and Singleton)

        Dagger2 Advanced-Scope Source Code Exploration

        Dagger2 Advanced - Briefly say SubComponent and Qualifier

        Project source code portal

    Because SubComponent and Qualifier are relatively simple, they are grouped into one chapter.

How to use SubComponent

    We create a new interface ThirdActivity and ThirdModule, ThirdCompnent:

    

@Module
public interface ThirdModule {

}
@Subcomponent(modules = ThirdModule.class)
public interface ThirdCompnent {
    void inject(ThirdActivity thirdActivity);
}

Then extend the AppComponent to add child components:

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
    void inject(MyApplication myApplication);

    ThirdCompnent addSub(ThirdModule thirdModule);

    //Tell the dependent Component that I gave you this class. than essential.
    Goods getGoods();
}

Finally, create an object in the ThirdActivity request:

public class ThirdActivity extends AppCompatActivity {

    @Inject
    Person mPerson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_third);

        ThirdCompnent thirdCompnent=MyApplication.newInsatnce().getAppComponent().addSub(new ThirdModule());
        thirdCompnent.inject(this);

        Log.d("ThirdActivity",mPerson.toString());
    }
}
The print shows that the object is available.

Interpretation of SubComponent source code

By compiling, we see that ThirdCompnent addSub(ThirdModule thirdModule); this sentence is generated in DaggerAppComponent


From the above source code, we can see that the sub-component of the template code generates an implementation class, and assigns values ​​to the Activity by calling the inject method of the implementation class layer by layer. In fact, the principle is the same.

The difference between SubComponent and Component

1. Component needs to tell the dependent modules that the classes that require open permissions, that is, to display dependencies, such as Goods

    SubComponent usage requires adding SubComponent-modified components to Component.

2. SubComponent reduces a lot of Dagger_XX classes, but the parent Component needs to hold the dependencies of SubComponent, which cannot be obtained in multi-component applications.

Component usage is shown in the figure:


Subomponent as shown:


Use of Qualifiers

@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Name {
    String value()  default "";
}

@Name("value")
    @Provides
    Order provideOrder2(Goods goods){
        Order order=new Order(goods);
        order.setName("My other Order");
        return order;
    }

@Name("value")
@Inject
Order mOrder2;

@Qualifier cannot be used directly and needs to be redefined.

@Qualifier

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529909&siteId=291194637