Results 1 to 5 of 5

Thread: Spring destroyScoped bean???

  1. #1
    Join Date
    Jun 2011
    Posts
    3

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    And what happens if you get it again and the bean isn't there for the current session... Spring creates a new one...
    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
    Jun 2011
    Posts
    3

    Default

    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?

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

    Default

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

  5. #5
    Join Date
    Jun 2011
    Posts
    3

    Default

    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!

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
  •