
DaggerBattleComponent . Place the cursor on DaggerBattleComponent and press Ctrl + B (or Ctrl + LMB, Command + LMB). You will see the following: @Generated( value = "dagger.internal.codegen.ComponentProcessor", comments = "https://google.imtqy.com/dagger" ) public final class DaggerBattleComponent implements BattleComponent { private DaggerBattleComponent(Builder builder) {} public static Builder builder() { return new Builder(); } public static BattleComponent create() { return new Builder().build(); } // @Override public War getWar() { return new War(new Starks(), new Boltons()); } public static final class Builder { private Builder() {} public BattleComponent build() { return new DaggerBattleComponent(this); } } } BattleComponent - the interface that we previously created and described in it the getWar() method to provide an instance of the class War .builder pattern. About this template is more fashionable to read here and here .getWar() method. Now I want to add a couple more dependencies: Starks and Boltons . Add methods to the interface: @Component interface BattleComponent { War getWar(); // Starks getStarks(); Boltons getBoltons(); } DaggerBattleComponent class. If you did everything right, you will see the following. @Generated( value = "dagger.internal.codegen.ComponentProcessor", comments = "https://google.imtqy.com/dagger" ) public final class DaggerBattleComponent implements BattleComponent { private DaggerBattleComponent(Builder builder) {} public static Builder builder() { return new Builder(); } public static BattleComponent create() { return new Builder().build(); } @Override public War getWar() { return new War(getStarks(), getBoltons()); } @Override public Starks getStarks() { return new Starks(); } @Override public Boltons getBoltons() { return new Boltons(); } public static final class Builder { private Builder() {} public BattleComponent build() { return new DaggerBattleComponent(this); } } } getStarks() and getBoltons() methods.@Inject annotation in the Boltons class. Let's break something. Remove the @Inject annotation from the Boltons class. Build the project again.
getWar() and getBoltons() methods will not work if there are no annotations with @Inject or @Provides .@Module and @Provides@Module and @Provides . They should be used if the size of your project increases.@ModuleContextModule module and this module will provide ApplicationContext and Context dependencies for other classes. To do this, we need to mark the ContextModule class with the @Module annotation.@ProvidesContextModule class with the @Module annotation, but we also need to label the methods that provide the ApplicationContext and Context dependencies with the @Provides annotation. public class Cash { public Cash(){ // - } } public class Soldiers { public Soldiers(){ // - } } BraavosModule . He will supply us with two dependencies - Cash and Soldiers . @Module // public class BraavosModule { Cash cash; Soldiers soldiers; public BraavosModule(Cash cash, Soldiers soldiers){ this.cash=cash; this.soldiers=soldiers; } @Provides // Cash Cash provideCash(){ return cash; } @Provides // Soldiers Soldiers provideSoldiers(){ return soldiers; } } @Module annotation, and methods that provide dependencies with the @Provides annotation.BattleOfBastards class and let the component implement the provideCash() and provideSoldiers() methods. @Component(modules = BraavosModule.class) interface BattleComponent { War getWar(); Cash getCash(); Soldiers getSoldiers(); } public class BattleOfBastards { public static void main(String[] args){ Cash cash = new Cash(); Soldiers soldiers = new Soldiers(); BattleComponent component = DaggerBattleComponent .builder() .braavosModule(new BraavosModule(cash, soldiers)) .build(); War war = component.getWar(); war.prepare(); war.report(); // component.getCash(); component.getSoldiers(); } } @Component . This suggests that the component will contain this module within itself.@Component(modules = BraavosModule.class).create() method of the .create() class. It arose due to the fact that when you add a module, you must pass this dependency to Dagger 2. It looks like this:BattleComponent component = DaggerBattleComponent.builder().braavosModule(new BraavosModule(cash, soldiers)).build();Component .component.getCash(); component.getSoldiers();DaggerBattleComponent and press Ctrl + B (or Ctrl + LMB, Command + LMB). You will see that the BraavosModule module BraavosModule included in the class to provide the Cash and Soldiers dependencies.builder pattern to provide dependencies. We also looked at a simple example of using the @Module and @Provides .Source: https://habr.com/ru/post/344886/
All Articles