简体   繁体   中英

Dagger 2 singleton not working

We are creating a dagger 2 dependency graph

SessionComponent (Session scope) --dependson---> Appcomponent (Singleton scope) ---dependson---> UserMangerComponent (unscoped... suppose to be singleton)

However, when I inject userManager (a dependency provided by UserManagerComponent), It is not being maintained as singleton. Every injection is creating a new UserManager. Please help ...

I cannot set singleton scope to the dependency.

Your proposed structure is incompatible with how Dagger manages scope. Only one component in your app should ever be @Singleton because each binding within a component that is not exposed via a component interface might be @Singleton , but entirely encapsulated in the component implementation. Thus, each component would hold its own instance and you'd end up with 2 instances rather than one.

Either merge the two components into one @Singleton component, create a new scope for your user management, or implement the instance management for your user manager by hand.

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