Android - Dagger 2:无法为通过应用程序组件提供的依赖项提供单个
Android - Dagger 2: Unable to provide a single for dependencies provided via Application Component
通过应用程序组件提供的依赖项没有单个实例。
我有以下代码:
@Component (modules = AppModule.class)
public interface AppComponent {
SharedPreferences getSharedPreferences();
}
@Module
public class AppModule {
private Context appContext;
private String prefFile;
public AppModule(@NonNull Context appContext, @NonNull String prefFile) {
this.appContext = appContext;
this.prefFile = prefFile;
}
@Provides
public SharedPreferences providePreferences(){
return new AppSharedPreferences(appContext, prefFile);
}
}
@Singleton
public class AppSharedPreferences implements SharedPreferences{
public AppSharedPreferences(Context appContext, String prefFile)
//Some code
}
public class AppApplication extends Application {
private AppComponent appComponent;
private InteractorsComponent interactorsComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder().appModule(new AppModule(getApplicationContext(), "PREF_STORE_NAME")).build();
interactorsComponent = DaggerInteractorsComponent.builder().appComponent(appComponent).build();
}
public AppComponent getAppComponent() {
return appComponent;
}
public InteractorsComponent getInteractorsComponent() {
return interactorsComponent;
}
}
@Module
abstract class InteractorsModule {
@Singleton
@Binds
abstract InteractorA provideInteractorA(AppInteractorA interactor);
@Singleton
@Binds
abstract InteractorB provideInteractorB(AppInteractorB interactor);
}
@Singleton
@Component (modules = InteractorsModule.class, dependencies = AppComponent.class)
public interface InteractorsComponent {
InteractorA getInteractorA();
InteractorB getInteractorB();
}
@Singleton
class AppInteractorA implements InteractorA {
private AppSharedPreferences pref;
@Inject
public AppInteractor(@NonNull AppSharedPreferences pref) {
this.pref = pref;
}
//Other overridden methods
}
@Singleton
class AppInteractorB implements InteractorB {
private AppSharedPreferences pref;
@Inject
public AppInteractor(@NonNull AppSharedPreferences pref) {
this.pref = pref;
}
//Other overridden methods
}
现在,InteractorA 和 InteractorB 将注入其他 classes。
InteractorA 和 InteractorB 实例本身在被注入的地方都是单例的。但是在 InteractorA 的对象和 InteractorB 的对象中,提供了不同的 SharedPreferences class 实例。所以 InteractorA 有一个不同的 SharedPreferences 实例,InteractorB 有一个不同的 SharedPreferences 实例。
有人可以帮助确保 InteractorA 和 InteractorB 具有相同的 SharedPreferences 实例。
谢谢。
@Module
public class AppModule {
// ...
@Provides
public SharedPreferences providePreferences(){
return new AppSharedPreferences(appContext, prefFile);
}
}
虽然 AppSharedPreferences 被注释为 @Singleton
,但您是在自己调用其构造函数,这意味着 Dagger 无法保证您的应用程序中只有一个 SharedPreferences 实例。这意味着无论何时注入 SharedPreferences,包括通过 InteractorsComponent 组件依赖项,您都会获得一个新的 AppSharedPreferences 实例。
您应该使用 @Singleton 标记 providePreferences
,或在 AppSharedPreferences 上使用 @Inject
构造函数,以便 Dagger 可以管理其 @Singleton
行为。
通过应用程序组件提供的依赖项没有单个实例。 我有以下代码:
@Component (modules = AppModule.class)
public interface AppComponent {
SharedPreferences getSharedPreferences();
}
@Module
public class AppModule {
private Context appContext;
private String prefFile;
public AppModule(@NonNull Context appContext, @NonNull String prefFile) {
this.appContext = appContext;
this.prefFile = prefFile;
}
@Provides
public SharedPreferences providePreferences(){
return new AppSharedPreferences(appContext, prefFile);
}
}
@Singleton
public class AppSharedPreferences implements SharedPreferences{
public AppSharedPreferences(Context appContext, String prefFile)
//Some code
}
public class AppApplication extends Application {
private AppComponent appComponent;
private InteractorsComponent interactorsComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder().appModule(new AppModule(getApplicationContext(), "PREF_STORE_NAME")).build();
interactorsComponent = DaggerInteractorsComponent.builder().appComponent(appComponent).build();
}
public AppComponent getAppComponent() {
return appComponent;
}
public InteractorsComponent getInteractorsComponent() {
return interactorsComponent;
}
}
@Module
abstract class InteractorsModule {
@Singleton
@Binds
abstract InteractorA provideInteractorA(AppInteractorA interactor);
@Singleton
@Binds
abstract InteractorB provideInteractorB(AppInteractorB interactor);
}
@Singleton
@Component (modules = InteractorsModule.class, dependencies = AppComponent.class)
public interface InteractorsComponent {
InteractorA getInteractorA();
InteractorB getInteractorB();
}
@Singleton
class AppInteractorA implements InteractorA {
private AppSharedPreferences pref;
@Inject
public AppInteractor(@NonNull AppSharedPreferences pref) {
this.pref = pref;
}
//Other overridden methods
}
@Singleton
class AppInteractorB implements InteractorB {
private AppSharedPreferences pref;
@Inject
public AppInteractor(@NonNull AppSharedPreferences pref) {
this.pref = pref;
}
//Other overridden methods
}
现在,InteractorA 和 InteractorB 将注入其他 classes。 InteractorA 和 InteractorB 实例本身在被注入的地方都是单例的。但是在 InteractorA 的对象和 InteractorB 的对象中,提供了不同的 SharedPreferences class 实例。所以 InteractorA 有一个不同的 SharedPreferences 实例,InteractorB 有一个不同的 SharedPreferences 实例。
有人可以帮助确保 InteractorA 和 InteractorB 具有相同的 SharedPreferences 实例。
谢谢。
@Module
public class AppModule {
// ...
@Provides
public SharedPreferences providePreferences(){
return new AppSharedPreferences(appContext, prefFile);
}
}
虽然 AppSharedPreferences 被注释为 @Singleton
,但您是在自己调用其构造函数,这意味着 Dagger 无法保证您的应用程序中只有一个 SharedPreferences 实例。这意味着无论何时注入 SharedPreferences,包括通过 InteractorsComponent 组件依赖项,您都会获得一个新的 AppSharedPreferences 实例。
您应该使用 @Singleton 标记 providePreferences
,或在 AppSharedPreferences 上使用 @Inject
构造函数,以便 Dagger 可以管理其 @Singleton
行为。