• 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

Spring singleton please explain

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have one question :How singleton behaves in the below code snippet :Assume I have Person POJO and I have defined the bean in the configuration file as :



As we know by default the spring bean's scope is singleton .When we call for a spring context (by using context.getBean("b1") and context.getBean("b2") ) we should get the same beans , but the fact is spring creating two different beans .

If anybody give any pointer on this it is really helpful .

Thanks ,
Satya



 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spring's singleton behaviour is on a per bean basis. It guarantees that for every time you retrieve a bean by name, say "p1" you will always get the same instance of the defined class. However, as you've discovered, if you define a second bean using the same class then Spring will create a second instance of that class for the second bean.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim for the short and accurate explanation .

Spring's singleton behavior is on a per bean basis

,, if we compare with real singleton pattern then what is the benefit/advantage(s) of spring singleton ?
I guess it is Thread safe ,, correct me if am wrong .
One quick question , can we make different objects (same as b1 , b2 defined in the configuration file ) by using annotation configuration ?

Regards,
Satya

 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Majumder wrote:if we compare with real singleton pattern then what is the benefit/advantage(s) of spring singleton ?


They are the same, therefore no comparison can be drawn.

S Majumder wrote:I guess it is Thread safe ,, correct me if am wrong .


Incorrect. There is nothing about the singleton pattern that guarantees thread safety.

S Majumder wrote:One quick question , can we make different objects (same as b1 , b2 defined in the configuration file ) by using annotation configuration ?


I don't know, but I suspect not.

It's an unlikely scenario where you would configure two identical beans of different names. There's just no need for it.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Majumder wrote:if we compare with real singleton pattern then what is the benefit/advantage(s) of spring singleton ?



Spring singleton's aren't Singletons. They are single instances. http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes.

The advantage of having a spring singleton scoped bean is that you can have multiple singleton. Actually Singleton design pattern has a big limitation. You can have only one instance of a Singleton. This is usually not a problem in most applications, until you reach a point where you do need multiple instances, and you realize that you have dug yourself in a Singleton hole. Spring allows you to have a single instance of a bean, which works in most cases. However, when you do need multiple, you can simply add it to the configuration.You are not digging yourself in a singleton hole by using spring singleton


I guess it is Thread safe ,, correct me if am wrong .



Spring singleton isn't more thread-safe than Singleton. You should make sure your beans do not share state between threads, and if they do share state, you should protect the state. Using Spring doesn't make your code thread-safe.


One quick question , can we make different objects (same as b1 , b2 defined in the configuration file ) by using annotation configuration ?

Regards,
Satya



Yes. You will need to use Spring Java configuration



By default, @Bean uses the method name to set the bean name. You can overrride that behavior by specifying it in the annotation http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for your view on this ..
Jayesh ,,

You should make sure your beans do not share state between threads, and if they do share state, you should protect the state.

can you explain a little detail on this .

I have a confusion of your code (in the last part of your post)

Why you are explicitly creating ,, it is kind of tight coupling .As we know spring will create / instantiate beans for us ..Can you give your view on this guys ..


Satya

 
reply
    Bookmark Topic Watch Topic
  • New Topic