Java Dagger 2-提供具有自己依赖项的方法
我正在努力理解和实施匕首2。我已经阅读了很多不同的教程和官方指南。我想我大体上理解了它,但我仍然不能理解一些简单的观点(虽然我写了它,但我已经找到了一些解决方案,但是..): @Provides方法可能有自己的依赖项 什么时候可能?Java Dagger 2-提供具有自己依赖项的方法,java,android,dependency-injection,dagger-2,Java,Android,Dependency Injection,Dagger 2,我正在努力理解和实施匕首2。我已经阅读了很多不同的教程和官方指南。我想我大体上理解了它,但我仍然不能理解一些简单的观点(虽然我写了它,但我已经找到了一些解决方案,但是..): @Provides方法可能有自己的依赖项 什么时候可能? 我所看到的可能是“组件包含一个依赖循环” 有没有人能帮助我了解可能的情况和不可能的情况。 谢谢。经过漫长的实验,我找到了答案。 我把这个答案作为自己的提示卡,希望它能帮助其他匕首绝地 所以我们有匕首结构 雄激素应用 基本活动 导航器 应用程序组件 应用程序模块
我所看到的可能是“组件包含一个依赖循环” 有没有人能帮助我了解可能的情况和不可能的情况。
谢谢。经过漫长的实验,我找到了答案。 我把这个答案作为自己的提示卡,希望它能帮助其他匕首绝地 所以我们有匕首结构
- 雄激素应用
- 基本活动
- 导航器
- 应用程序组件
- 应用程序模块
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
Navigator navigator();
Context context();
//...
}
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
BaseNavigator navigator(); // changed to interface type
Context context();
//...
}
ApplicationModule.class
@Module
public class ApplicationModule {
private final AndroidApplication application;
public ApplicationModule(AndroidApplication application) {
this.application = application;
}
@Provides
@Singleton
Navigator provideNavigator() {
return new Navigator();
}
@Provides
@Singleton
Context provideApplicationContext() {
return this.application;
}
}
@Module
public class ApplicationModule {
private final AndroidApplication application;
public ApplicationModule(AndroidApplication application) {
this.application = application;
}
@Provides
@Singleton
BaseNavigator provideNavigator(Navigator navigator) {
return navigator;
} // this will return interface type but with implementation you needed
@Provides
@Singleton
Context provideApplicationContext() {
return this.application;
}
}
导航器类
@Singleton
public class Navigator implements BaseNavigator {
public Navigator() {}
}
@Singleton
public class Navigator implements BaseNavigator {
@Inject // don't forget to add this annotation to the constructor
public Navigator() {}
}
基本活动类
public abstract class BaseActivity extends Activity {
@Inject
Navigator navigator;
//code here
}
public abstract class BaseActivity extends Activity {
@Inject
BaseNavigator navigator;// changed to interface type
//code here
}
此代码将正常工作,BaseActivity将以ApplicationModule提供的新navigator()的形式获取navigator
但是,如果您有几个BaseNavigator类的实现,您可以获得一些特定的实现,例如Navigator类,而无需手动创建新实例
*此构造将为您提供“组件包含依赖循环”
您可以这样做: ApplicationComponent.class
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
Navigator navigator();
Context context();
//...
}
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
BaseNavigator navigator(); // changed to interface type
Context context();
//...
}
ApplicationModule.class
@Module
public class ApplicationModule {
private final AndroidApplication application;
public ApplicationModule(AndroidApplication application) {
this.application = application;
}
@Provides
@Singleton
Navigator provideNavigator() {
return new Navigator();
}
@Provides
@Singleton
Context provideApplicationContext() {
return this.application;
}
}
@Module
public class ApplicationModule {
private final AndroidApplication application;
public ApplicationModule(AndroidApplication application) {
this.application = application;
}
@Provides
@Singleton
BaseNavigator provideNavigator(Navigator navigator) {
return navigator;
} // this will return interface type but with implementation you needed
@Provides
@Singleton
Context provideApplicationContext() {
return this.application;
}
}
导航器类
@Singleton
public class Navigator implements BaseNavigator {
public Navigator() {}
}
@Singleton
public class Navigator implements BaseNavigator {
@Inject // don't forget to add this annotation to the constructor
public Navigator() {}
}
基本活动类
public abstract class BaseActivity extends Activity {
@Inject
Navigator navigator;
//code here
}
public abstract class BaseActivity extends Activity {
@Inject
BaseNavigator navigator;// changed to interface type
//code here
}
现在您没有为Navigator创建新的实例,Dagger在其生成的工厂中代替了您创建了它。实际上,您可以使用限定符(
@Named(“something”)
注释)为给定的依赖项获取多种不同类型的实现
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
@Named("first")
BaseNavigator firstNavigator();
@Named("second")
BaseNavigator secondNavigator();
Context context();
//...
}
@Module
public class ApplicationModule {
private final AndroidApplication application;
public ApplicationModule(AndroidApplication application) {
this.application = application;
}
@Provides
@Singleton
@Named("first")
BaseNavigator provideFirstNavigator() {
return new SomeNavigator();
}
@Provides
@Singleton
@Named("second")
BaseNavigator provideSecondNavigator() {
return new OtherNavigator();
}
@Provides
Context provideApplicationContext() {
return this.application;
}
}
public abstract class BaseActivity extends Activity {
@Inject
@Named("second")
BaseNavigator navigator;
Navigator provideNavigator(Navigator-Navigator){
当然,它包含一个循环。provider方法需要它自己的结果作为依赖项,这将是一个递归方法调用。如果需要提供Navigator
字段,则应该将其提供给模块本身(尽管我不明白为什么),或者您不应该将其指定为提供程序方法的参数。但是我想如果您想要实现的是使用@Inject
带注释的构造函数将实例化移出模块,那么我想这是可行的。