Results 1 to 5 of 5

Thread: Best approach to create prototype within Singleton

  1. #1
    Join Date
    Aug 2004
    Posts
    107

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    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

  3. #3
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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
    Alef Arendsen
    SpringSource
    http://www.springsource.com

  4. #4
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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
    Alef Arendsen
    SpringSource
    http://www.springsource.com

  5. #5
    Join Date
    Aug 2004
    Posts
    107

    Default

    Interesting approaches.

    thx.

    Dino

Similar Threads

  1. Replies: 4
    Last Post: Oct 5th, 2005, 11:04 AM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. Replies: 6
    Last Post: May 25th, 2005, 01:56 AM
  5. DefaultAdvisorAutoProxyCreator skipping beans
    By youngm in forum Container
    Replies: 6
    Last Post: Apr 12th, 2005, 04:29 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
  •