开发者

Why isn't guice injecting the previously-instantiated @SessionScoped object?

开发者 https://www.devze.com 2023-02-08 07:00 出处:网络
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

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 sess开发者_StackOverflow社区ion). 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消