简体   繁体   中英

Android Dagger2, Singleton cross Component

i have a problem with Singleton with the Dagger2 library for Android. My problem is im using the @Singleton but getting two different objects =[

i Have 2 Components and 2 Modules:

DispatcherComponent which includes the DispatcherModule class that provides a Dispatcher. the Dispatcher needs instance UserStore which is provided by StoreModule.

@Singleton
@Component(modules={AppModule.class, StoreModule.class, DispatcherModule.class})
public interface DispatcherComponent {
    void inject(SomeClass someClass);
}

and the DispatcherModule.class is as follows

@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

@Singleton
@Component(modules={AppModule.class, StoreModule.class})
public interface StoreComponent {
    void inject(SomeOtherClass otherClass);
}

and 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 =/

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 ! 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. Check DaggerDispatcherComponent for more detail.

For this case, you can change StoreModule like below:

@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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM