Announcement Announcement Module
Collapse
No announcement yet.
Prototype or Container-Aware? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Prototype or Container-Aware?

    I have a situation where I need to create an undetermined number of instances of a particular class. Rather than programatically call getBean() and be container-aware I am considering injecting one instance and cloning it.

    I believe this would be following the Prototype pattern, staying container-agnostic and following the Dependency Injection approach.

    What are your thoughts?

    Thanks in advance,
    codeGorilla

  • #2
    I would suggest prototype - hiding the instance management is one of the advantages of IoC - the component does not have to be aware of how its dependencies are created/retrieved.

    Comment


    • #3
      Originally posted by codegorilla
      I have a situation where I need to create an undetermined number of instances of a particular class. Rather than programatically call getBean() and be container-aware I am considering injecting one instance and cloning it.

      I believe this would be following the Prototype pattern, staying container-agnostic and following the Dependency Injection approach.

      What are your thoughts?

      Thanks in advance,
      codeGorilla
      You might also want to look at method injection: http://static.springframework.org/sp...thod-injection

      Essentially you define a Bean getYourBean() on your class and everytime you call getYourBean() spring will essentially factory.call getBean() for you.

      Maybe that would be better than cloning because all the dependencies on yourBean will be wired up for you.

      Just a thought.

      Comment


      • #4
        Thanks for the feedback. I'll review the method injection approach and compare this with using a prototype. I will be needing this solution in several places throughout my project. My main concern now will be maintainability... which approach is easier to follow.

        I'll post my results soon,
        codeGorilla

        Comment


        • #5
          Another technique I've used is to have a property which takes a org.springframework.beans.factory.ObjectFactory. You can inject an instance of this using a ObjectFactoryCreatingFactoryBean in your XML file. Its a good alternative to using BeanFactoryAware. It is still Spring specific, but less intrusive than BeanFactoryAware and doesn't require your object be clonable.

          Although I don't often like my code to be Spring specific, there are a few places where this sometimes happens
          • Data Access
          • JMS
          • Special factories, particularly if my prototypes takes arguments when they are created

          But I still strive to keep my domain objects and stateless services container agnostic. And when I can't, its usually a sign for a collaborator to be created to hide these details behind an interface.

          Comment


          • #6
            Originally posted by codegorilla
            Thanks for the feedback. I'll review the method injection approach and compare this with using a prototype. I will be needing this solution in several places throughout my project. My main concern now will be maintainability... which approach is easier to follow.

            I'll post my results soon,
            codeGorilla
            To be clear; the method injection will inject a bean that in your case should be defined as singleton=false, i.e. the bean will be a prototype.

            Just so we are on the same page

            Comment

            Working...
            X