Announcement Announcement Module
Collapse
No announcement yet.
Spring destroyScoped bean??? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring destroyScoped bean???

    I am using

    configurableListableBeanFactory.destroyScopedBean( "MyForm")


    to clear the MyForm bean from the session(it's a session scope spring bean).

    After destroying the bean in the very next line I did,

    MyForm myForm= (MyForm)configurableListableBeanFactory.getBean("M yForm");

    I was expecting the myForm instance to be null but it's not instead it's still an object but all the instance members of MyForm are now null!

    shouldn't the myForm object been completely removed from the session(thus becoming available for GC) instead of it's instance member's values changed to null?

    Thanks

  • #2
    And what happens if you get it again and the bean isn't there for the current session... Spring creates a new one...

    Comment


    • #3
      Hello Marten,

      So you mean, that once a bean has been created by spring, then if i use that been in destroyScopedBean. After destroying the bean if I get that bean again using getBean method then i would be returned a newly created object of that bean?

      Can you also explain the use case of such scenario, as in traditional applications after we do session.removeAttribute, the attribute is no longer available, but here it's not the same with spring?

      Comment


      • #4
        So you mean, that once a bean has been created by spring, then if i use that been in destroyScopedBean. After destroying the bean if I get that bean again using getBean method then i would be returned a newly created object of that bean?
        Correctly...

        Can you also explain the use case of such scenario, as in traditional applications after we do session.removeAttribute, the attribute is no longer available, but here it's not the same with spring?
        Destroying a bean and afterwards getting it ( a new instance) isn't the same as you describe, you are comparing apples and oranges here... Spring uses an ApplicationContext which is a BeanFactory, a Factory which creates Beans. Now if you do a getBean (for a scoped one) it first checks if the bean is available for that scope (in this case it checks the session), you removed it hence not available so spring creates a new scoped instance..

        Comment


        • #5
          Thanks Martem for the reply, now I understand how it works.

          The applicationContext once it loads records all the beans of the all the scopes that are bound to it. If i destroy once of the bean and again get it, it returns me a new object of that bean as that bean is part of the applicationContext xml declarations. But if i do getBean("NewBeanName") would throw an exception as NewBeanName was not part of the applicationContext that loaded all the beans!

          Comment

          Working...
          X