Dagger2 メモ
n 個の @Component から 1 個のインスタンスに inject
不可
public class Main { @Inject Red red; @Inject Blue blue; public Main() { DaggerBlueComponent.builder().blueModule(new BlueComponent.BlueModule()).build().inject(this); DaggerRedComponent.builder().redModule(new RedComponent.RedModule()).build().inject(this); } }
public class Blue { } @BlueScope @Component(modules = BlueComponent.BlueModule.class) public interface BlueComponent { void inject(Main main); @Module class BlueModule { @BlueScope @Provides Blue provideBlue() { return new Blue(); } } } @Scope @Retention(RUNTIME) public @interface BlueScope { }
public class Red { } @RedScope @Component(modules = RedComponent.RedModule.class) public interface RedComponent { void inject(Main main); @Module class RedModule { @RedScope @Provides Red provideRed() { return new Red(); } } } @Scope @Retention(RUNTIME) public @interface RedScope { }
:sample:compileDebugJavaWithJavac /Users/shogo/dev/code/android/myrepos/android-tasting-dagger2/sample/src/main/java/com/sho5nn/sample/di/BlueComponent.java:13: エラー: com.sho5nn.sample.di.Red cannot be provided without an @Inject consttor or from an @Provides- or @Produces-annotated method. void inject(Main main); ^ com.sho5nn.sample.di.Red is injected at com.sho5nn.sample.Main.red com.sho5nn.sample.Main is injected at com.sho5nn.sample.di.BlueComponent.inject(main) /Users/shogo/dev/code/android/myrepos/android-tasting-dagger2/sample/src/main/java/com/sho5nn/sample/di/RedComponent.java:13: エラー: com.sho5nn.sample.di.Blue cannot be provided without an @Inject consttor or from an @Provides- or @Produces-annotated method. void inject(Main main); ^ com.sho5nn.sample.di.Blue is injected at com.sho5nn.sample.Main.blue com.sho5nn.sample.Main is injected at com.sho5nn.sample.di.RedComponent.inject(main) エラー2個
@Scope ( @RedScope
, @BlueScope
) を持っている/持っていないは関係ない
n 個の @Component に依存した 1 個の @Component から 1 個のインスタンスに inject
可能
public class Main { @Inject Red red; @Inject Blue blue; public Main() { BlueComponent blueComponent = DaggerBlueComponent.builder() .blueModule(new BlueComponent.BlueModule()).build(); RedComponent redComponent = DaggerRedComponent.builder() .redModule(new RedComponent.RedModule()).build(); DaggerColorComponent.builder() .blueComponent(blueComponent) .redComponent(redComponent) .build() .inject(this); } }
@ColorScope @Component(dependencies = {BlueComponent.class, RedComponent.class}) public interface ColorComponent { void inject(Main main); } @Scope @Retention(RUNTIME) public @interface ColorScope { }
//@BlueScope @Component(modules = BlueComponent.BlueModule.class) public interface BlueComponent { Blue blue(); @Module class BlueModule { // @BlueScope @Provides Blue provideBlue() { return new Blue(); } } } //@RedScope @Component(modules = RedComponent.RedModule.class) public interface RedComponent { Red red(); @Module class RedModule { // @RedScope @Provides Red provideRed() { return new Red(); } } }
依存先 @Component 全てが @Scope ( @RedScope
, @BlueScope
) を持っていると不可
:sample:compileDebugJavaWithJavac /Users/shogo/dev/code/android/myrepos/android-tasting-dagger2/sample/src/main/java/com/sho5nn/sample/Main.java:5: エラー: シンボルを見つけられません import com.sho5nn.sample.data.DaggerBlueComponent; ^ シンボル: クラス DaggerBlueComponent 場所: パッケージ com.sho5nn.sample.data /Users/shogo/dev/code/android/myrepos/android-tasting-dagger2/sample/src/main/java/com/sho5nn/sample/Main.java:6: エラー: シンボルを見つけられません import com.sho5nn.sample.data.DaggerRedComponent; ^ シンボル: クラス DaggerRedComponent 場所: パッケージ com.sho5nn.sample.data /Users/shogo/dev/code/android/myrepos/android-tasting-dagger2/sample/src/main/java/com/sho5nn/sample/Main.java:9: エラー: シンボルを見つけられません import com.sho5nn.sample.domain.DaggerColorComponent; ^ シンボル: クラス DaggerColorComponent 場所: パッケージ com.sho5nn.sample.domain /Users/shogo/dev/code/android/myrepos/android-tasting-dagger2/sample/src/main/java/com/sho5nn/sample/domain/ColorComponent.java:10: エラー: @com.sho5nn.sample.domain.ColorScope com.sho5nn.sample.domain.ColorComponent depends on more than one scoped component: @Component(dependencies = {BlueComponent.class, RedComponent.class}) ^ @com.sho5nn.sample.data.BlueScope com.sho5nn.sample.data.BlueComponent @com.sho5nn.sample.data.RedScope com.sho5nn.sample.data.RedComponent エラー4個
depends on more than one scoped component