-
1. Re: Understanding @Inject vs @Inject Instance<> and .get()
luksa Mar 27, 2012 4:21 AM (in response to christopherp)You have only one instance of MainView, so its field usersSSB can only point to one instance of SSB.
Of course, if the field is an Instance<SSB>, it also points to only one instance of Instance<SSB>. However, calling .get() on it can return different instances of SSB, depending on the scope - that's why you get different SSBs for each user/session.
-
2. Re: Understanding @Inject vs @Inject Instance<> and .get()
christopherp Mar 28, 2012 6:03 PM (in response to luksa)Hi Marko, thanks for the reply! I'm honestly having trouble wrapping my head around this. There is a new MainView created for each application, and each application is a SessionScoped bean created by the servlet. Here's some more code that might explain what's happening (I apologize for the cross-posting from SO, I just wanted to see if a weld expert might be able to figure this out too):
The Application class is being injected into the Servlet on a new HttpServletRequest:
public abstract class AbstractCdiApplicationServlet extends
AbstractApplicationServlet {
@Inject
protected Instance<ApplicationWrapper> wrapper;
@Override
protected Application getNewApplication(HttpServletRequest request)
throws ServletException {
return wrapper.get().getApplication();
}
...etc etcAnd the
ApplicationWrapper
is aSessionScoped
bean:@SuppressWarnings("serial")
@SessionScoped
public class ApplicationWrapper implements Serializable {
@Inject
private AbstractCdiApplication application;
public AbstractCdiApplication getApplication() {
return application;
}
}Doesn't this mean that calling
@Inject SSB usersSSB
anywhere in MainView (or any object within that user's session) should give me that user's session-scoped bean, and always that same session-scoped bean, for each user's session? Meaning -- different usersSSB for different users, because each user has a different session.After all, the
Application
itself is aSessionScoped
bean, injected into and attached to the user's HTTP Session by thegetNewApplication
method of the servlet? I mean, it is the Application object that injects and attaches the MainView class, after all, right? So that means MainView is a session-scoped bean, doesn't it?I'm just trying to figure out how this all works, I guess. Thanks for the help!