title: how spring singleton scope for DAO class works internally
body: <p>I went through some blogs and spring docs about the Spring singleton scope along with almost all spring singleton and DAO related question in stackoverflow.
  I still do not have clear understanding of how the same object is injected to all the class which depend on it. I have learnt that the DAO needs to be <strong>stateless</strong>.</p>
  
  <p>If the following DAO (sample dao having instance variable mainly to clear confusion) class is defined with default singleton scope and the same object is injected everytime, then there might be scenarios where department is null and therefore it won't set anything for department value  instead use whatever the previous object value was.</p>
  
  <pre><code>public class UserDAO{
  
      int userId;
      Spring userDepartment;
      // getter setter methods for userId and userDepartment
  
     public boolean addUserToUserDetailsTable(int uId, 
                                              String name, String address, String department){
          // set userId
          userId = uId;
  
          if(department!=null)
             userDepartment = department;
  
         // write code to add user to user table
         // TO DO 
  
  
         // save user department data
         addUserToUserDepartmentTable(userId, userDepartment);
     }
  
  
  
     public void addUserToUserDepartmentTable(int uId, 
                                                 String department){
          /* Code to save department data */
     }
  
  }
  </code></pre>
  
  <p>So if instead of using <strong>DI</strong>, if I manually call the DAO using new operator this problem won't be there.</p>
  
  <p>new UserDAO().addUserToUserDetailsTable(id, "abc", null);</p>
  
  <p>the above confusion generates following questions</p>
  
  <ol>
  <li><p>how is spring creating and injecting singelton beans, is it really one and only one object which gets injected to all calling classes. If this is true then how the previous object values from above DAO class is reset.</p></li>
  <li><p>won't the instance variable hold their values here userId, userDepartment if the same object is called from multiple class ?? Does stateless means the class cannot have instance variable.</p></li>
  <li><p>does spring internally uses new object() to inject the beans.</p></li>
  <li><p>or it creates an object of DAO class and makes multiple clones of the object, which i think is not possible because the DAO class is not implementing clonnable.</p></li>
  </ol>
  
  <p>Please help me clearing the above confusion.</p>