Announcement Announcement Module
Collapse
No announcement yet.
Managed httpsession objects ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Managed httpsession objects ?

    Is it possible to have spring set objects from a http session on other objects ?

    For example in a simple shoppingcart system I'd like to have an action with a method like this

    setShoppingCart(Cart cart)

    where cart is stored in the http session

  • #2
    Spring, and DI in general, can set objects/properties from accessible existing Objects or from objects that can be created using some bean instantication / objects factories.
    If i understood your question, you need to extract an object from the HttpSession and then inject it into an other object. Well, could you please elaborate more as of:
    1. when will this be done? at your application startup ? then there is no HttpSession started yet...
    2. will you configure the DI using an applicationContext? AFAIK there is no mean to reference HttpSession from within objects configured in applicationContext
    3. you can use a Filter/servlet that extract the cart and put it in a ThredLocal and configure a factory that will read the cart and reference this factory in the destination object. But, is this worth the effort?

    Comment


    • #3
      1. when will this be done? at your application startup ? then there is no HttpSession started yet...
      obviously

      2. will you configure the DI using an applicationContext? AFAIK there is no mean to reference HttpSession from within objects configured in applicationContext
      That was kind of the implicit question, and the reason I initially posted this message in the core container forum. I figured there might be some build-in way to handle these things. using ioc/dep injection for session objects seams like a nice clean way to handle dependancies on session objects

      3. you can use a Filter/servlet that extract the cart and put it in a ThredLocal and configure a factory that will read the cart and reference this factory in the destination object. But, is this worth the effort?
      I am using spring with webwork and it has a threadlocal action context from which you can retrieve among other things the session map
      So writing a filter isn't necessary. Making things a lot easier.

      Since I apperently have to come up with my own method I guess I could
      take two approaches


      1) create a “singleton” proxy bean for the session object that delegates calls to the real object it stores in the session and retrieves it using a threadlocal

      2) create the object using a static factory method that creates session objects

      Code:
      public static Object createInstance(String beanPrototypeName, String sessionkey)
      Code:
      <bean id="ShoppingCart" class="ShoppingCart" singleton="false" />
      
      <property name="shoppingCart">
        <bean class="SessionObjectFactory" factory-method="createInstance" singleton="false">
          <constructor-arg>
            <idref local="ShoppingCart" />
          </constructor-arg>
           ...
        </bean>
      </property>
      That seems like a cleaner approach
      That way I could prototype the session object in the application context
      and I wouldn't have to have seperate interface, implementation, proxy classes

      Let me know what you think

      Comment

      Working...
      X