简体   繁体   English

匕首2单身人士无法正常工作

[英]Dagger 2 singleton not working

We are creating a dagger 2 dependency graph 我们正在创建一个匕首2依赖图

SessionComponent (Session scope) --dependson---> Appcomponent (Singleton scope) ---dependson---> UserMangerComponent (unscoped... suppose to be singleton) SessionComponent(会话作用域)--dependson ---> Appcomponent(单作用域)--- dependson ---> UserMangerComponent(未作用域...假定为单例)

However, when I inject userManager (a dependency provided by UserManagerComponent), It is not being maintained as singleton. 但是,当我注入userManager(由UserManagerComponent提供的依赖项)时,它并没有保持为单例。 Every injection is creating a new UserManager. 每次注入都会创建一个新的UserManager。 Please help ... 请帮忙 ...

I cannot set singleton scope to the dependency. 我无法将单例作用域设置为依赖项。

Your proposed structure is incompatible with how Dagger manages scope. 您提出的结构与Dagger管理范围的方式不兼容。 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. 应用程序中只有一个组件应该是@Singleton因为未通过组件接口公开的组件中的每个绑定都可能是@Singleton ,但完全封装在组件实现中。 Thus, each component would hold its own instance and you'd end up with 2 instances rather than one. 因此,每个组件都将拥有自己的实例,最终您将拥有2个实例,而不是一个。

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. 可以@Singleton两个组件合并为一个@Singleton组件,为用户管理创建新作用域,或者手动为用户管理器实现实例管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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