为什么 guice 不注入之前实例化的 @SessionScoped 对象?

发布于 2024-10-15 18:15:54 字数 238 浏览 2 评论 0原文

我有一个@SessionScoped?被注入到 Stripes 框架拦截器构造函数中的 DAO 似乎是从拦截器中找到的(在后续调用中),但没有被注入到同一请求(和会话)中的服务中。为什么同一个实例(在拦截器中初始化)不在服务中重用(位于同一项目的不同包中)?

将 DAO 设为 @Singleton 可以解决问题,但这是不可接受的,因为 DAO 存储的信息必须在应用程序上的用户会话期间保持一致,而该应用程序具有共享同一 DAO 实例的多个用户。

I have a @SessionScoped? DAO that's being injected into a Stripes framework Interceptor constructor that seems to be found from the interceptor (on subsequent calls) but is not being injected into a service in the same request (and session). Why isn't the same instance (initialized in the interceptor) being reused in the service (which is in a different package in the same project)?

Making the DAO a @Singleton does the trick, but is unacceptable as the DAO stores information that must remain consistent throughout a user's session on an application that has multiple users who would be sharing the same DAO instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟火散人牵绊 2024-10-22 18:15:54

如果 Interceptor 不是会话范围的对象,那么您需要将 Provider 注入 Interceptor 中。当一个寿命较长的对象依赖于另一个寿命较短的对象时,这是常用的模式。

If the Interceptor is not a session-scoped object, then you'll need to inject Provider<YourDaoType> into the Interceptor. This is the common pattern to use when an object with a long life depends on another object with a shorter life.

温馨耳语 2024-10-22 18:15:54

好的,我已经弄清楚了。我将 @SessionScoped 更改为用于注入的 bind(DAO.class).in(ServletScopes.SESSION) 语句。据我了解,这些应该是等效的,但就我而言,它们产生了不同的结果。

一路上困扰我的一个方面是,Stripes 构建了在启动时注入 DAO 的拦截器,导致错误,因为这种情况发生在会话范围之外(DAO 是 @SessionScoped)。 ActionBeanContext 上下文信息需要初始化 DAO 会话上下文,我在构造操作 bean 期间调用的 AbstractActionBean setContext 方法中设置了该上下文。

感谢您的关注和帮助。

ok, I've figured it out. I changed the @SessionScoped to a bind(DAO.class).in(ServletScopes.SESSION) statement with which the injection works. As far as I understand, these should be equivalent, but in my case they're producing different results.

One aspect that was troubling me along the way was that Stripes built the interceptor that injected the DAO on startup causing errors as this happened outside the scope of a session (which the DAO is @SessionScoped. ActionBeanContext context information is needed to initialize the DAO session context which I set in an AbstractActionBean setContext method that is called during the construction of the action bean.

Thanks for your interest and help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文