Announcement Announcement Module
Collapse
No announcement yet.
Removing session scoped beans from the session Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Removing session scoped beans from the session

    Hi,

    Is there a standard way to clean up session-scoped beans? I suppose I could write a destroy method to clear all fields in the bean but I think it would be better to remove the bean entirely.

    Thanks

  • #2
    Hi

    The Spring container's scoping infrastructure will automatically remove xxx-scoped beans from the relevant backing resource. So in the case of HTTPSession-scoped beans, the container will do the removing for you, without any explicit action on your part.

    There is however nothing stopping you doing this removal yourself explicitly. Do you have some specific scenario that would require this?

    Cheers
    Rick

    Comment


    • #3
      Hi,

      I need to remove the bean from the session explicitly. It begins as an empty object but gradually increases in size as the user goes through multiple form submissions. Once the form is submitted, I don't want to keep these values around anymore. Users will often submit this form multiple times in a single session so the values either need to be reset after completion or (better) the bean needs to be removed from the session.

      I know that spring stores beans in the session using a prefix of "scopedTarget", but I'm hoping there's a better way to explicity remove them.

      Thanks

      Comment


      • #4
        Hi

        Mmm, I see your point now.

        The ConfigurableBeanFactory interface has a destroyScopedBean(String beanName) method declared on it. If you can get a handle onto the underlying ConfigurableBeanFactory implementation instance, you will be able to do the explicit destruction in one call.

        Cheers
        Rick

        Comment


        • #5
          Hmm..seems like it should work but the AbstractBeanFactory throws an IllegalArgumentException. I know the bean is in the session scope because getBean("beanName") returns the bean wrapped by CGLIB.

          ((ConfigurableBeanFactory)webApplicationContext.ge tAutowireCapableBeanFactory()).destroyScopedBean(" beanName");

          Any other suggestions?


          Thanks

          Comment


          • #6
            Hiya

            Darn. Ok, I'll go look at the unit test(s) and see if I can reproduce said error.

            Cheers
            Rick

            Comment

            Working...
            X