• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

basic doubt regarding beans

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am new to spring with practically no hands on experience with EJB. I am not sure whether this question even makes sense.

While reading Manning Spring in Action I came across the singleton property of the bean - prototype | singleton. Googling a bit more on this here, I read:

Beans are defined to be deployed in one of two modes: singleton or non-singleton. (The latter is also called a prototype, although the term is used loosely as it doesn't quite fit). When a bean is a singleton, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned.

The non-singleton, prototype mode of a bean deployment results in the creation of a new bean instance every time a request for that specific bean is done. This is ideal for situations where for example each user needs an independent user object or something similar.

Beans are deployed in singleton mode by default, unless you specify otherwise. Keep in mind that by changing the type to non-singleton (prototype), each request for a bean will result in a newly created bean and this might not be what you actually want. So only change the mode to prototype when absolutely necessary



Now the question: does bean instance corresponds to the bean object (speaking in java terms)?
if yes, then in singleton deployment of bean, I will get the reference to the same object again and again and that makes it useless for things like student bean where I would like to create N number of students? So can you give some example where I would go for singleton beans.


Sorry for this long explanation of my problem, but was not sure how to put this all in words...

Thanks

 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In practice most beans controlled/configured by Spring are singleton beans because the state is always in the database or web session rather than in the beans. Most Spring MVC web applications are built in three layers - web, service and persistence. Web controllers can be singleton beans - they pull all their data from request/session/application scopes and from the service layer below them. Service layer beans can be singletons because they just take requests and get data from the persistence layer beans below them. Persistence layer beans take requests and run queries to return model objects. These model objects are the things that hold real data / state, and if they are always saved in the database or in the session scope of the web container, none of your spring configured beans will need to.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic