[英]Android Dagger2, Singleton cross Component
i have a problem with Singleton with the Dagger2 library for Android. 我在Android Dagger2库中遇到Singleton问题。 My problem is im using the @Singleton but getting two different objects =[
我的问题是即时通讯使用@Singleton但得到两个不同的对象= [
i Have 2 Components and 2 Modules: 我有2个组件和2个模块:
DispatcherComponent which includes the DispatcherModule class that provides a Dispatcher. DispatcherComponent,其中包括提供Dispatcher的DispatcherModule类。 the Dispatcher needs instance UserStore which is provided by StoreModule.
Dispatcher需要由StoreModule提供的UserStore实例。
@Singleton
@Component(modules={AppModule.class, StoreModule.class, DispatcherModule.class})
public interface DispatcherComponent {
void inject(SomeClass someClass);
}
and the DispatcherModule.class is as follows 并且DispatcherModule.class如下
@Module
public class DispatcherModule {
@Provides
@Singleton
public Dispatcher provideDispatcher(UserStore store) {
Log.d("DEBUG", "userStore : " + store.toString());
return new Dispatcher(store);
}
and now the StoreComponent.class 现在是StoreComponent.class
@Singleton
@Component(modules={AppModule.class, StoreModule.class})
public interface StoreComponent {
void inject(SomeOtherClass otherClass);
}
and StoreModule.class 和StoreModule.class
@Module
public class StoreModule {
@Provides
@Singleton
public UserStore provideUserStore() {
return new UserStore();
}
now when im trying to inject UserStore im getting two different objects =/ 现在当我试图注入UserStore我得到两个不同的对象= /
public class SomeOtherClass extends Acitivity {
@Inject UserStore mStore;
public void onCreate(Bundle savedInstance) {
StoreComponent comp = ((MyApp) getApplication).getStoreComponent();
comp.inject(this);
Log.d("DEBUG", "userStore2 :" + mStore.toString());
}
}
public class SomeClass {
@Inject Dispatcher mDispatcher;
public SomeClass (Application application) {
((MyApp) application).getDispatcherComponent().inject(this);
}
and last, this is how i create the components: 最后,这就是我创建组件的方式:
public class MyApp extends Application {
public void onCreate() {
StoreModule store = new StoreModule();
StoreComponent storeComponent = DaggerStoreComponent.builder().appModule(new AppModule(this)).storeModule(storeModule).build();
DispatcherComponent disComp = DaggerDispatcherComponent.builder().appModule(new AppModule(this)).storeModule(storeModule).dispatcherModule(new DispatcherModule()).build();
}
now, when im running the Application, i get 2 different objects ! 现在,当我运行应用程序时,我得到2个不同的对象! can someone help me ?
有人能帮我吗 ? how should i fix it?
我该如何解决? i dont want to have a god component..
我不想有上帝的组成部分。
THanks! 谢谢!
Note that @Singleton dost not make the object to be singleton actually, instead, it just use the DoubleCheck
class to cache which is hold by the component impl generated by dagger. 请注意,@ Singleton实际上并不会使对象成为单例对象,而是仅使用
DoubleCheck
类来缓存由dagger生成的组件impl持有的对象。 Check DaggerDispatcherComponent for more detail. 有关更多详细信息,请检查DaggerDispatcherComponent。
For this case, you can change StoreModule like below: 在这种情况下,您可以如下更改StoreModule:
@Module
public class StoreModule {
private UserStore mStore;
@Provides
@Singleton
public UserStore provideUserStore() {
return getStore();
}
private UserStore getStore() {
//lazy initialized and make sure thread safe
if (null == mStore) {
@synchronized(StoreModule.class) {
if (null == mStore) {
mStore = new UserStore();
}
}
}
return mStore;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.