Results 1 to 3 of 3

Thread: Question on Session Scope

  1. #1
    Join Date
    Nov 2009
    Posts
    6

    Default Question on Session Scope

    Does anyone know, when I define a bean to be session scoped, would that implicitly create a session (getSession(true)) even if no session existed at the given time?

    In other words, I want a bean to be request scoped if no session is available, and move that bean to session scope whenever the application decides to create a session (e.g. after a user login).

    Background: I don't want the application to have a user session by default. A loggedInUserCredentials bean would however be injected to the controller - being initialized as "anonymous" from scratch. When a user logs in the app will create a session (getSession(true)), and from that point on the loggedInUserCredentials bean should reside in session scope.

    Any tricks or legal methods to accomplish this?

    Thank you all, br,
    Ernest

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Session scope is called session scope for a reason . It requires a session to be active.

    Also from your short description it seems to me that you want to simply store something in the session, the user object (or credentials) why not simply do it yourself and retrieve it where needed?

    Basically if you want the behavior as described you would need to implement yuor own scope (or modify the existing scope)
    Last edited by Marten Deinum; Mar 19th, 2010 at 05:51 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2009
    Posts
    6

    Default

    Thanks for your answer Marten.
    Actually what I want to achieve is to have a "generic" user credentials bean injected into the controller that is either request based (if no user session is available) or session based (if a user session exists).

    I believe I could more easily handle that using a custom BeanFactory which would have request scope? This factory could then look if a session is available and inject the bean from there if it exists, or create and store a request scoped bean. By any means this would effectively hide the logic from the controller code (thats what I intend).

    Only issue here - at request start the factory finds there is no session, so it creates a request-scoped bean. Later during handling of the request the application decides to open a session - from this moment on the bean would need to be located in session storage. How could the BeanFactory become aware of the fact that now a session is available?

    Thanks for insights,
    Ernest

Tags for this Thread

Posting Permissions

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