Results 1 to 10 of 11

Thread: A couple of problems with session scope beans

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Location
    Toronto, ON, Canada
    Posts
    7

    Default A couple of problems with session scope beans

    Hi, folks,

    a couple of questions from a Spring MVC newbie...

    First, I have a problem trying to inject a session scope bean into a controller.

    Here's the bean config:

    Code:
    <bean id="userSessionContainer" class="UserSessionContainerImpl" scope="session">
            <aop:scoped-proxy />
    </bean>
    
    <bean id="loginController" class="com.condosmart.web.controller.LoginController">
    ...
            <property name="userSessionContainer" ref="userSessionContainer" />
    ...
    </bean>
    Controller class:

    Code:
    private UserSessionContainer userSessionContainer;
    
    public void setUserSessionContainer(UserSessionContainer userSessionContainer) {
        this.userSessionContainer = userSessionContainer;
    }
    
    protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
    //... (userByLogin is loaded here)
    userSessionContainer.setPrincipal(userByLogin);
    //...
    }
    And now I want to display, say, the name of the Principal in a jsp:

    Code:
    ${userSessionContainer.principal.name}
    and nothing is displayed!

    If instead of using the <aop:scoped-proxy> i+ setter injection I just get the bean from the Application Context:

    Code:
    UserSessionContainer userSessionContainer =
    (UserSessionContainer)getApplicationContext().getBean(Constants.USER_SESSION_CONTAINER_BEAN_ID);
    
    userSessionContainer.setPrincipal(userByLogin);
    everything works fine.

    Actually, I have also noticed that when I try to access any session-scoped bean from a jsp using JSTL:
    Code:
    ${sessionBean.blah}
    I don't get anything. And as soon as I get this bean somewhere in a controller
    Code:
    applicaitonContext.getBean("sesionBeanId")
    JSTL tags do get it.

    I am definitely missing something here... any advice is greatly appreciated.

    Yours sincerely,
    Andrey.
    Last edited by ashulinsky; Apr 18th, 2007 at 09:13 AM.

  2. #2

    Default

    No object is referred if nothing is displayed in a JSP file. You need to give your object a name in your controller and refer it in your JSP file. You can study more those samples in the distributed package to understand how to get it work.

    HTH.

  3. #3
    Join Date
    Mar 2007
    Location
    Toronto, ON, Canada
    Posts
    7

    Default

    Thanks for the prompt reply

    Quote Originally Posted by vw729 View Post
    No object is referred if nothing is displayed in a JSP file. You need to give your object a name in your controller and refer it in your JSP file. You can study more those samples in the distributed package to understand how to get it work.
    Right, the session object is not even initialized until I explicitly do applicationContext.getBean(...).

    However, the question is - why isn't it? Is it the way it's supposed to work? I thought that any session object would be created and put to any new session automatically. I have also tries setting lazy-init="false" to no result.

    In regards to the setting injection of a session bean to a singleton controller through <aop:scoped-proxy> - in this case a proxy is created, I can work with it within the controller but - again - don't have it in a jsp. Once again, the real session object doesn't seem to be created - and I thought it would after reading this part of the reference:

    http://static.springframework.org/sp...scopes-session

    Rather what you need then is to inject some sort of object that exposes the exact same public interface as the UserPreferences class (ideally an object that is a UserPreferences instance) and that is smart enough to be able to go off and fetch the real UserPreferences object from whatever underlying scoping mechanism we have chosen (HTTP request, Session, etc.). We can then safely inject this proxy object into the 'userManager' bean, which will be blissfully unaware that the UserPreferences reference that it is holding onto is a proxy. In the case of this example, when a UserManager instance invokes a method on the dependency-injected UserPreferences object, it is really invoking a method on the proxy... the proxy will then go off and fetch the real UserPreferences object from (in this case) the HTTP Session, and delegate the method invocation onto the retrieved real UserPreferences object.

    So, where is the catch?

    Yours sincerely,
    Andrey.

  4. #4
    Join Date
    Mar 2007
    Posts
    13

    Default

    <<property name="userSessionContainer" ref="userSessionContainer" />

    You have double "<" in your config.

  5. #5
    Join Date
    Mar 2007
    Location
    Toronto, ON, Canada
    Posts
    7

    Default

    Quote Originally Posted by mwang2008 View Post
    <<property name="userSessionContainer" ref="userSessionContainer" />

    You have double "<" in your config.
    Sorry, it's a copy-paste problem, the real config is fine.

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    It's instantiated as soon as it is needed. If you have your controller set up as singleton with a dependency on the session-scope proxy the proxy gets injected on initialization of the controller. You should be able to debug that one or log it in the setUserSessionContainer(..). The actual session-scoped object gets instantiated on the first access to the proxy. This should be your userSessionContainer.setPrincipal(userByLogin). I can't see something wrong in your code, so you probably need to debug or log if those both methods are called.

    Jörg

  7. #7
    Join Date
    Mar 2007
    Location
    Toronto, ON, Canada
    Posts
    7

    Default

    Jorg,

    thanks, sounds like session-scope beans are always "lazy-initialized" and there's no way to instantiate them automatically on a session's creation. If so, it means that a session-scope bean can not be used in a jsp until the bean is referenced (and as a result instantiated) somewhere in the code. Right?

    Regarding the injection - I did some debugging and the userSessionContainer.setPrincipal(userByLogin) method was successfully executed. The userSessionContainer.getPrincipal() invocation did return the user set by the setter. Still, it seems all this happened to the proxy, the real object had not been instantiated - neither its constructor nor the setter were called. And - again - no reference in the jsp...

    Yours sincerely,
    Andrey.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •