Results 1 to 3 of 3

Thread: How to clone a bean?

  1. #1
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default How to clone a bean?

    Was looking at some options I have for reusing an object and was considering cloning an existing object since the new copy differs in one field.

    I think I can get around the use of cloning. But, now I'm wondering how object cloning would be done using Spring. getBean either creates a new object or reuses an existing one, right.

    I haven't looked at all the object factories yet. Maybe there is something that does this already, of course.

  2. #2

    Default Re: How to clone a bean?

    Two possible approaches:

    create several beans (different ids) of the same class at set their attributes in your beans.xml/applicationcontext.xml.

    or make the bean singleton="false" so you get a new instance of the class each time you call getBean(), have Spring set all the common values and your code set the ones that differ.

  3. #3
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default Re: How to clone a bean?

    Quote Originally Posted by Edward Kenworthy
    Two possible approaches:

    create several beans (different ids) of the same class at set their attributes in your beans.xml/applicationcontext.xml.

    or make the bean singleton="false" so you get a new instance of the class each time you call getBean(), have Spring set all the common values and your code set the ones that differ.
    I too thought that the second option was viable. Since one reason for using clone is to avoid reconstruction of expensive contained objects, these objects can just be referenced as singleton beans in Spring.

    Would the framework friction add more cost and thus eliminate any advantage of using clone? BTW, in a simple test, clone did not prove faster then 'new', probably a bad test.

    Another option is storing a factory in the context; this factory would have, in addition to the create methods, a clone method that takes run-time parameters.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •