Results 1 to 4 of 4

Thread: Session state in Spring

  1. #1
    Join Date
    Mar 2007
    Posts
    15

    Default Session state in Spring

    Hi, I'm trying out Spring MVC, and I could use some advice regarding storing session state and using it in Controllers (to customize rendered views for example).
    Firstly I assumed that I'd use session scoped bean and inject it as a controller property. Id did not work out at first as container complained that controller has different scope than a session bean, and that's fair enough. The googled solution was to use following syntax:

    <bean id="userSession" class="UserSessionImpl" scope="session">
    <aop:scoped-proxy/>
    </bean>

    As I understand the '<aop:scoped-proxy/>' configures the container to actually create a single wrapper proxy that routes call to bean methods to proper session bean. This way the singelton controller bean can interact with session beans. Is this how it's suppose be done? Btw, there is no way to configure the same with annotation (without JavaConfig) is there?

    Also, I have more general question, is it a good practice to use them statefull session beans with Spring MVC for purpose of, well, storing some session-valid state (shopping cart or such)? Can anyone advice on this?

    Thanks, a lot
    Łukasz

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

    Default

    As I understand the '<aop:scoped-proxy/>' configures the container to actually create a single wrapper proxy that routes call to bean methods to proper session bean. This way the singelton controller bean can interact with session beans. Is this how it's suppose be done? Btw, there is no way to configure the same with annotation (without JavaConfig) is there?
    That is indeed the correct configuration. If you want this with annotations simply use @Scope on your bean.

    Also, I have more general question, is it a good practice to use them statefull session beans with Spring MVC for purpose of, well, storing some session-valid state (shopping cart or such)? Can anyone advice on this?
    Sure why not, that is what they are for. Although I like to call them Session scoped beans, session beans have such a negative ring to them .
    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
    Mar 2007
    Posts
    15

    Default

    Marten, thanks for clarification.

    Quote Originally Posted by Marten Deinum View Post
    That is indeed the correct configuration. If you want this with annotations simply use @Scope on your bean.
    actually I meant the '<aop:scoped-proxy/>' tag, not scope==session. I googled it, and it seems that the only way to configure it with annotation is by using @ScopedProxy, and that requires using JavaConfig configuration method.

    Anyway, thanks a lot for help.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    @Scope also works in conjunction with context:component-scan. The @ScopedProxy is the javaconfig variant of that (registering also the mechanism that creates scoped proxies which is also done by the context tag mentioned earlier).
    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

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
  •