Announcement Announcement Module
Collapse
No announcement yet.
Why spring beans are Singleton by default? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why spring beans are Singleton by default?

    I have a basic question, may be I am missing here something.

    To my understanding the Spring beans are singleton by default. This is single bean instance is created per context.

    I am not sure how is this managing the concurrency? In my web application there may be multiple users who are trying to access the same code and modifying the data.

    In EJB it is handled by having pool of components, there is no singleton. In Servlet it is handled by multi threading the service methods.

    I would like to understand how is this managed in Spring beans.

  • #2
    Nobody force you to leave scope at its default.
    Anyway, a lot of objects in the application are singletons (datasources, DAOs, etc.), really any stateless object may be singleton without causing any concurrency issues.

    Originally posted by goudh View Post
    I have a basic question, may be I am missing here something.

    To my understanding the Spring beans are singleton by default. This is single bean instance is created per context.

    I am not sure how is this managing the concurrency? In my web application there may be multiple users who are trying to access the same code and modifying the data.

    In EJB it is handled by having pool of components, there is no singleton. In Servlet it is handled by multi threading the service methods.

    I would like to understand how is this managed in Spring beans.

    Comment


    • #3
      You have to consider, that Spring is not intended for wiring your domain objects. And these are usually the ones having state.

      With Spring you usually wire collaborating beans which contain processing logic. In most cases these beans are stateless, hence there is no concurrency issue here.

      Regards,
      Andreas

      Comment


      • #4
        see also:
        http://forum.springframework.org/showthread.php?t=47723

        Comment


        • #5
          Thank you informed me.

          Comment

          Working...
          X