• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple ApplicationContext and Singleton

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to have multiple ApplicationContext initialized in a single class?
How does a bean with singleton bean behave then?
What is the difference between non-spring singleton (created via singleton pattern or so)and singleton bean (that is scoped so in spring)?
 
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akhilesh Trivedi wrote:Is it possible to have multiple ApplicationContext initialized in a single class?
How does a bean with singleton bean behave then?
What is the difference between non-spring singleton (created via singleton pattern or so)and singleton bean (that is scoped so in spring)?



You can initialize multiple ApplicationContexts. And of course you can hold references to different contexts in a single class.

A Spring singleton is a singleton in its context; otherwise it can be a simple class without any protection against multiple instantiation. You can create different objects of the class manually but if you define a Spring bean as singleton then the context will always return the same object. If you have multiple contexts you can have multiple objects but they are singletons in the context in which they are defined singleton.

If you use the singleton pattern on a language level, i.e. by using private constructors and static getters and fields, the class can't be instantiated more than once on a language/VM level.

Honestly, singletons on a language level are something similar to global variables to me. They make testing hard, they make debugging hard, they make extension hard, they introduce global state. They may be needed for the execution environment of the VM but for the application programmer they are mostly a sign of laziness. Singletons on an application level are something completely different. When you define a bean as singleton semantically (e.g. in a Spring context) you avoid those problems. So if you are using Spring then you don't need to worry how to implement singletons. You can use them but not on a language level.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic