Dagger: override @ method-method in the regional module

Suppose the following situation. The main version of the application has a global module AppModule, a cloud module ScopedModule, a class, Mainand a class Foo. In addition, there is a debugging option with a module DebugAppModule, module, DebugScopedModuleand class Bar. Only a debugging option can know about Bar.

The main option contains the following relevant code excerpts.

@Module AppModule { /*..*/ }

@Module(injects=Main.class, addsTo=AppModule.class)
ScopedModule { @Provides Foo provideFoo() { return new Foo(); } }

class Main { scopedGraph = graph.plus(new ScopedModule(this)); }
class Foo { /*..*/ }

// In the entry point of the application
ObjectGraph.create(new AppModule());

The debug option contains the following relevant code excerpts.

@Module(addsTo=AppModule.class, overrides=true) DebugAppModule { /*..*/ }

@Module(injects=Main.class, addsTo=DebugAppModule.class, overrides=true)
DebugScopedModule { @Provides Foo provideFoo() { return new Bar(); } }

class Bar extends Foo { /*..*/ }

// In the entry point of the application
ObjectGraph.create(new AppModule(), new DebugAppModule());

, @Provides , .. plus . ., , . Foo Foo, Bar. , Main ScopedModule ( new).

, - -, :). AppModule ScopedModule Main. , ScopedModule Main, AppModule Main, (, , Android, Main Activity).

, @Provides ?

+5
1

Dagger @Provided .

. @vaughandroid

, , .

MyComponent component = DaggerMyComponent.builder()
.appModule(new AppModule() {
  @Override public Foo provideFoo() { 
      return new Bar(); 
  }
})
.build();  

, , .

0

Source: https://habr.com/ru/post/1568901/


All Articles