具有混合作用域的模块
Modules with mixed scopes
我正在尝试实现以下行为:
- 在具有不同作用域的另一个模块中使用单例作用域下的模块提供的对象。
这是我的资料。我已经根据几个答案尝试了很多更改,但我仍然无法解决这个问题。
第一个模块(应该绑定到应用程序的生命周期)
@Module
public class AModule {
private Context context;
public AModule(Context context) {
this.context = context;
}
@Provides
@Singleton
MySharedPreference provideMySharedPreference(SharedPreferences prefs) {
return new MySharedPreferences(prefs);
}
@Provides
@Singleton
SharedPreference provideSharedPreference() {
return context.getSharedPreferences("prefs", 0);
}
是组件
@Component(modules = AModule.class)
@Singleton
public interface AComponent {
void inject(...);
}
第二个模块(绑定到Activity生命周期)
@Module
public class BModule {
@Provides
@ActivityScope
X provideX(MySharedPreferences prefs) {
return new Y(prefs);
}
}
是组件
@Component(modules = BModule.class)
@ActivityScope
public interface BComponent {
Y Y();
}
我声明了Activity范围
@Scope
@Retenion(RetentionPolicy.RUNTIME)
public @interface ActivityScope{}
而MySharedPreferences如下
public class MySharedPreferences {
private SharedPreferences mSharedPrefs;
@Inject
public MySharedPreferences(SharedPreferences prefs) {
mSharedPrefs = prefs;
}
// some methods
}
最后,在我的应用程序 class 中,我创建了 A 组件
aComponent = DaggerAComponent.builder().aModule(new AModule(getApplicationContext())).build();
编辑 我试过的一些东西
我尝试将 includes = AModule.class
添加到 BModule。
我尝试将 dependencies = AComponent.class
添加到 BComponent。
我尝试使用 ActivityScope 注释创建一个新组件。
如果您正在使用依赖组件 (dependencies =
),您需要编写一个提供方法来公开从 @Singleton
作用域组件到 @ActivityScope
组件的依赖关系。
@Component(modules = AModule.class)
@Singleton
public interface AComponent {
void inject(...);
SharedPreferences exposeSharedPreferences();
}
提供方法将允许从属 @ActivityScope
组件使用 @Singleton
绑定 SharedPreferences
:
@Component(modules = BModule.class, dependencies = AComponent.class)
@ActivityScope
public interface BComponent {
Y Y();
}
有关提供方法的更详细说明,请参阅 。
我正在尝试实现以下行为:
- 在具有不同作用域的另一个模块中使用单例作用域下的模块提供的对象。
这是我的资料。我已经根据几个答案尝试了很多更改,但我仍然无法解决这个问题。
第一个模块(应该绑定到应用程序的生命周期)
@Module
public class AModule {
private Context context;
public AModule(Context context) {
this.context = context;
}
@Provides
@Singleton
MySharedPreference provideMySharedPreference(SharedPreferences prefs) {
return new MySharedPreferences(prefs);
}
@Provides
@Singleton
SharedPreference provideSharedPreference() {
return context.getSharedPreferences("prefs", 0);
}
是组件
@Component(modules = AModule.class)
@Singleton
public interface AComponent {
void inject(...);
}
第二个模块(绑定到Activity生命周期)
@Module
public class BModule {
@Provides
@ActivityScope
X provideX(MySharedPreferences prefs) {
return new Y(prefs);
}
}
是组件
@Component(modules = BModule.class)
@ActivityScope
public interface BComponent {
Y Y();
}
我声明了Activity范围
@Scope
@Retenion(RetentionPolicy.RUNTIME)
public @interface ActivityScope{}
而MySharedPreferences如下
public class MySharedPreferences {
private SharedPreferences mSharedPrefs;
@Inject
public MySharedPreferences(SharedPreferences prefs) {
mSharedPrefs = prefs;
}
// some methods
}
最后,在我的应用程序 class 中,我创建了 A 组件
aComponent = DaggerAComponent.builder().aModule(new AModule(getApplicationContext())).build();
编辑 我试过的一些东西
我尝试将 includes = AModule.class
添加到 BModule。
我尝试将 dependencies = AComponent.class
添加到 BComponent。
我尝试使用 ActivityScope 注释创建一个新组件。
如果您正在使用依赖组件 (dependencies =
),您需要编写一个提供方法来公开从 @Singleton
作用域组件到 @ActivityScope
组件的依赖关系。
@Component(modules = AModule.class)
@Singleton
public interface AComponent {
void inject(...);
SharedPreferences exposeSharedPreferences();
}
提供方法将允许从属 @ActivityScope
组件使用 @Singleton
绑定 SharedPreferences
:
@Component(modules = BModule.class, dependencies = AComponent.class)
@ActivityScope
public interface BComponent {
Y Y();
}
有关提供方法的更详细说明,请参阅