Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Definition of Spring "singleton"?

  1. #1
    Join Date
    Feb 2006
    Posts
    164

    Default Definition of Spring "singleton"?

    Hi, everyone.

    I've looked through the documentation at Spring and in my books (Spring in Action and Pro Spring), but I can't find a clear definition for Spring's use of the word "Singleton".

    Is this a per-JVM singleton? per-thread singleton? or per-ApplicationContext-instance singleton? (There could be subtle but important differences here, regarding the classloader andThreadLocal.)

    Please just point me to the doc where this is explained, if I'm just missing something simple.

    Ben

  2. #2
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi Ben

    In the context of Spring, the 'singleton' that is referred to is a 'per-ApplicationContext-instance'.

    http://static.springframework.org/sp...-factory-modes

    Cheers
    Rick

  3. #3
    Join Date
    Feb 2006
    Posts
    164

    Default

    Thanks, Rick. Reading over the doc you showed, the answer still looks murky to me. Is that your opinion, or do you know that for sure? If so, how?

    ...or did I miss a significant phrase in the doc?

    Ben

  4. #4
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Quote Originally Posted by Rick Evans
    In the context of Spring, the 'singleton' that is referred to is a 'per-ApplicationContext-instance'.
    What about these in the same context?
    Code:
    <bean id="bar1" class="com.foo.Bar"/>
    <bean id="bar2" class="com.foo.Bar"/>
    Shouldn't those be two instances as well?
    --Jing Xue

  5. #5
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    per-ApplicationContext-instace for one bean. In your case you'll have two singletons - i.e two shared instances of the same class, one under bean bar1 and one under bar2.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Btw, for more info see some of the 'red' books from the main site - they offer a lengthy but thorough explanations on Spring and j2ee concepts.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  7. #7
    Join Date
    Jan 2006
    Location
    Spain
    Posts
    44

    Default

    Is there a wat to obtain a singleton shared by all ApplicacionContext-instances?

    Thanks in advance.

  8. #8
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    If all your applicationContexts are already sharing the singleton then all you have to do is get a hold of a context and simply request the singleton by its bean name.
    I think the problem is how to create multiple application contexts that share the same beans (i.e. you have a hierarchy of contexts distributed across different VMs). Right now there's no out of the box support in Spring and I think you have to use some commercial products which offer 'transparent' clustering to be able to do this.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  9. #9
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Quote Originally Posted by Costin Leau
    per-ApplicationContext-instace for one bean. In your case you'll have two singletons - i.e two shared instances of the same class, one under bean bar1 and one under bar2.
    Exactly - that's the point I was implying. Strictly speaking, it's not even per-ApplicationContext-intance, it's per-ApplicationContext-instance-and-per-bean. IMHO, the difference here is significant, theoretically at least.

    Outside Spring, whenever we talk about singletons, there is always an assumption that we are talking about how many instances are there per class - "a per-JVM singleton" means "one instance for this class throughout JVM", "a clustering singleton" means "one instance for this class throughout the 'world'". But in the context of Spring, that assumption isn't necessarily true any more - Spring does not distinguish or manage beans by their classes, but by ids or names. So, singleton="true" actually has some very different semantics from the usual singleton concept we as Java programmers have taken for granted.

    That may well seem to be some "hair splitting", but I think it can get very confusing especially for new Spring users.
    --Jing Xue

  10. #10
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Exactly - that's the point I was implying. Strictly speaking, it's not even per-ApplicationContext-intance, it's per-ApplicationContext-instance-and-per-bean. IMHO, the difference here is significant, theoretically at least.
    Fair dues... I agree that defining Spring's concept of a singleton in 100% clear, authoritative and unambiguous terms is important, and the reference doc section is not '100% clear, authoritative and unambiguous'.

    I'll address this in the 2.0 final timeframe. Feel free to chip in with suggested text

    http://opensource.atlassian.com/proj...rowse/SPR-2124

    Cheers
    Rick
    Last edited by Rick Evans; Jun 12th, 2006 at 01:27 AM.

Posting Permissions

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