Announcement Announcement Module
Collapse
No announcement yet.
Session state in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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 .

    Comment


    • #3
      Marten, thanks for clarification.

      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.

      Comment


      • #4
        @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).

        Comment

        Working...
        X