Announcement Announcement Module
Collapse
No announcement yet.
Best approach to create prototype within Singleton Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best approach to create prototype within Singleton

    I have a facade (which is a singleton) but my business layer object is not (i.e. is a prototype). E.g my facade delegates the logic to the business layer (my bl layer is injected into the facade).

    What is the best approach in this scenerio (I don't want to make my facade to be a prototype in order for my business lay to be a prototype)?

    Dino

  • #2
    You can define your prototype normally
    Code:
    <bean id="bizProt" singleton="false" ....>
    Then you code your singleton with a attribute holding a FactoryBean instead of your business class.
    Code:
    class MyFacade &#123;
      FactoryBean businessFactoryBean;
      void setBusinessFactoryBean&#40;FactoryBean fb&#41; &#123;
        ..
      &#125;
      ...
    &#125;
    and wire your factory returning a new business object in the singleton like this :
    Code:
    <bean id="myFacade" class="MyFacade">
      <property name="businessFactoryBean">
        <ref local="&amp;bizProt"/>
      </property>
    </bean>
    Then in your facade singleton, you can now call this.businessFactoryBean.getObject() to get a fresh instance.

    Does this solve your problem ?

    Olivier

    Comment


    • #3
      You have two options here.

      First of all you can use method injection. This comes down to implementing an (optionally) abstract method in your serivce object. Using a lookup method when defining your service object in the application context you will be given a new instance each time you invoke the specific method. Have a look at seciont 3.3.3.1 of the reference manual to see how this works.

      The second option is to use a PrototypeTargetSource. A target source can serve as a ProxyFactoryBean's target, creating a new object on each method invocation. There are other target sources as well, for example a PoolingTargetSource (using an arbitrary pooling strategy like Commons Pool). Target sources are explained in more detail in section 5.11 of the reference manual.

      Which of the two is the best depends on several things: the target source approach creates a new object for each method invocation whereas the lookup method injection approach only creates a new one when the lookup method is called. When applying the lookup method strategy however, you're somewhat tying yourself to Spring, since you're relying on Spring overriding the method. Testing outside the container will be a bit more difficult.

      Remember that it's best to prevent prototype from entering your application. Although it's not that expensive to create new objects, you might want to consider using a pooling strategy.

      regards,
      Alef

      Comment


      • #4
        About Olivier's approach; generally I wouldn't suggest to have a reference to a FactoryBean in your service object. It ties your object to Spring and makes testing in isolation (without Spring) more difficult.

        regards,
        Alef

        Comment


        • #5
          Interesting approaches.

          thx.

          Dino

          Comment

          Working...
          X