Results 1 to 7 of 7

Thread: Singleton and Prototype Caching

  1. #1
    Join Date
    Nov 2005
    Posts
    12

    Default Singleton and Prototype Caching

    I understand that spring caches singleton objects, but if I have a singleton object that has a dependency on a prototype, how do I get spring to inject the prototype into the singleton each time it brings back the cached instance of it. I thought this is what the BeanPostProcesses were doing?

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Quote Originally Posted by punnooser
    I understand that spring caches singleton objects, but if I have a singleton object that has a dependency on a prototype, how do I get spring to inject the prototype into the singleton each time it brings back the cached instance of it. I thought this is what the BeanPostProcesses were doing?
    I haven't looked too deeply into the depths of this, so I might be completely wrong.

    If beanA is marked as a singleton and beanB is a prototype, then everytime you ask Spring for beanA, it will return the same instance. Asking for beanB on the other hand will give you a new instance.

    When Spring resolves dependencies, I expect it honours this, so if you have beanC (as well as beanA) which depend on beanB then they will each get a new instance of beanB (the prototype) *but* that instance will never change, i.e. beanA.getBeanB() will *always* return the same instance.

    This is because beanA is a singleton and is only ever wired up once.

  3. #3
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    You can have a look at the Target sources, especially the Prototype target source.

  4. #4
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Take a look at Lookup method injection in the reference manual. That will allow your singleton to always return a new reference to your prototype object each call.
    Bill

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Of course; there is always the option to mark the original bean as a non-singleton.

    You need to be careful with threading issues when using combined singletons/prototypes....

  6. #6
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    To be honest, in most cases I don`t like the fact that something is done with a class if the class was not designed for it. If a class needs a new reference everytime, I make it expliciet by using some kind of factory. Now you can see what is going on. I can imagine there are cases you need to, but I don`t recommend making a habit from it.
    Last edited by Alarmnummer; Jan 31st, 2006 at 02:14 AM.

  7. #7
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    +1 from me.

    Knowing whether a class is singleton or not allows you to make assumptions about it's behaviour (caching, thread safety etc.) As soon as "part" of the class is no longer singleton; bad things may happen.

    As Alarmnummer says, the explicitness in this case would be very important IMHO.

Posting Permissions

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