title: Spring Boot : @Autowired remember and save the same object also for the next request
body: <p>I'm developing a spring boot application , below one API for example : </p>
  
  <pre><code>@Autowired
  GetUserIdByUserNameBase getUserIdByUserNameBase;
  
  @PostMapping(value = "/UserName")
      public GetUserIdByUserNameOutput getUserIdByUserName(@RequestBody String userName) {
          GetUserIdByUserNameInput GetUserIdByUserNameInput = new GetUserIdByUserNameInput(userName);
      getUserIdByUserNameBase.setGetUserIdByUserNameInput(GetUserIdByUserNameInput);
      MsgStatus msgStatus = getUserIdByUserNameBase.execute();
      if (msgStatus == MsgStatus.ERROR_MSG) {
          LOG.error("UserRestController,getUserIdByUserName : Internal System Error");
      }
      return getUserIdByUserNameBase.getGetUserIdByUserNameOutput();
  }
  </code></pre>
  
  <p>My issue appear in the second/next request with the same API ( for example the above one )
  "GetUserIdByUserNameBase" still remember all the saved data from the 
  first hit!.</p>
  
  <pre><code>public class GetUserIdByUserNameBase extends ServiceBase {
  
      private final Logger LOG = LogManager.getLogger(GetUserIdByUserNameBase.class);
      private GetUserIdByUserNameInput getUserIdByUserNameInput;
      private GetUserIdByUserNameOutput getUserIdByUserNameOutput;
      private ResultStatus resultStatus;
      private String userName;
      private TBUSER_INFO userInfo;
  </code></pre>
  
  <p>Here in my case, "getUserIdByUserNameOutput" still saving the previous 
  data from the first hit of API until I override the content or mark it as 
  empty.</p>
  
  <p>1 . GetUserIdByUserNameBase.setGetUserIdByUserNameOutput(new GetUserIdByUserNameOutput );</p>
  
  <p>or</p>
  
  <ol start="2">
  <li>GetUserIdByUserNameBase.getGetUserIdByUserNameOutput().setX(Null);</li>
  </ol>
  
  <p>but here I create one problem, Since I created a new instance of "GetUserIdByUserNameOutput" for every new API request and is not efficient and heavy for the system...</p>
  
  <p>If I summarise what i want really to know , how I can tell @Autowired 
  to bounce/rest the object on every new request of the API and not use or override what it remember from the first request.?</p>
  
  <p>Here My full code : </p>
  
  <pre><code>package com.usermanagementms.ims.servicesfacade.userservices;
  
  import javax.transaction.Transactional;
  
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.usermanagementms.ims.enums.MsgStatus;
  import com.usermanagementms.ims.repositories.userdao.TBUserInfoDao;
  import com.usermanagementms.ims.tables.user.TBUSER_INFO;
  import com.usermanagementms.ims.types.commontypes.ResultStatus;
  import com.usermanagementms.ims.types.getuseridbyusername.GetUserIdByUserNameInput;
  import com.usermanagementms.ims.types.getuseridbyusername.GetUserIdByUserNameOutput;
  import com.usermanagementms.ims.utils.ErrorMsg;
  import com.usermanagementms.ims.utils.ErrorsController;
  import com.usermanagementms.ims.utils.Utils;
  
  @Service
  public class GetUserIdByUserNameBase extends ServiceBase {
  
      private final Logger LOG = LogManager.getLogger(GetUserIdByUserNameBase.class);
      private GetUserIdByUserNameInput getUserIdByUserNameInput;
      private GetUserIdByUserNameOutput getUserIdByUserNameOutput;
      private ResultStatus resultStatus;
      private String userName;
          private TBUSER_INFO userInfo;
  
          @Autowired
          private TBUserInfoDao TBUserInfoDao;
  
          @Autowired
          private ErrorsController errorsController;
  
          @Transactional
          public TBUSER_INFO getUserIdByUserName(String username) {
  
              TBUSER_INFO userInfo;
              try {
                  userInfo = TBUserInfoDao.findIdByUserName(username);
                  if (userInfo == null) {
                      LOG.warn("UserService,getUserIdByUserName : userInfo is empty");
                      return null;
                  }
                  return userInfo;
              } catch (Exception e) {
                  LOG.error("UserService,getUserIdByUserName : error in retrieving user from DB { " + e.getMessage() + "}");
              }
              return null;
          }
  
          @Override
          public MsgStatus inputMapping() {
              MsgStatus msgStatus = MsgStatus.SUCCESS_MSG;
              this.setGetUserIdByUserNameOutput(new GetUserIdByUserNameOutput());
              if (getResultStatus() == null) {
                  this.setResultStatus(new ResultStatus());
              }
              msgStatus = super.inputMapping();
              if (msgStatus == MsgStatus.ERROR_MSG || this.getUserIdByUserNameInput == null) {
                  LOG.error("GetUserIdByUserNameBase,inpuMapping() : Internal System Error");
                  this.getErrorsController().fillResultStatus(ErrorMsg.SYSTEM_ERROR, this.resultStatus);
                  this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
                  return MsgStatus.ERROR_MSG;
              }
              this.setUserName(getUserIdByUserNameInput.getUserNameX1());
              return MsgStatus.SUCCESS_MSG;
          }
  
          @Override
          public MsgStatus validate() {
              if (Utils.isNullorEmpty(userName)) {
                  LOG.error("GetUserIdByUserNameBase,validate() : userName is null or empty");
                  this.getErrorsController().fillResultStatus(ErrorMsg.REQUEST_BODY_EMPTY, this.resultStatus);
                  this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
                  return MsgStatus.ERROR_MSG;
              }
              return MsgStatus.SUCCESS_MSG;
          }
  
          @Override
          public MsgStatus executeImpl() {
              MsgStatus msgStatus = MsgStatus.SUCCESS_MSG;
              TBUSER_INFO userInfo = getUserIdByUserName(userName);
              this.setUserInfo(userInfo);
              if (userInfo == null) {
                  msgStatus = this.getErrorsController().fillResultStatus(ErrorMsg.USER_NOT_FOUND, resultStatus);
                  if (msgStatus == MsgStatus.ERROR_MSG) {
                      this.getErrorsController().fillResultStatus(ErrorMsg.SYSTEM_ERROR, resultStatus);
                      this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
                      LOG.error("GetUserIdByUserNameBase,executeImpl() : Internal System Error");
                      return MsgStatus.ERROR_MSG;
                  }
                  this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
                  return MsgStatus.ERROR_MSG;
              }
              this.getErrorsController().fillResultStatus(ErrorMsg.OK, resultStatus);
              this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
              return MsgStatus.SUCCESS_MSG;
          }
  
          @Override
          public MsgStatus outputMapping() {
              this.getGetUserIdByUserNameOutput().setUserId(this.getUserInfo().getUid());
              return MsgStatus.SUCCESS_MSG;
          }
  
          public GetUserIdByUserNameInput getGetUserIdByUserNameInput() {
              return getUserIdByUserNameInput;
          }
  
          public void setGetUserIdByUserNameInput(GetUserIdByUserNameInput getUserIdByUserNameInput) {
              this.getUserIdByUserNameInput = getUserIdByUserNameInput;
          }
  
          public GetUserIdByUserNameOutput getGetUserIdByUserNameOutput() {
              return getUserIdByUserNameOutput;
          }
  
          public void setGetUserIdByUserNameOutput(GetUserIdByUserNameOutput getUserIdByUserNameOutput) {
              this.getUserIdByUserNameOutput = getUserIdByUserNameOutput;
          }
  
          public String getUserName() {
              return userName;
          }
  
          public void setUserName(String userName) {
              this.userName = userName;
          }
  
          public ErrorsController getErrorsController() {
              return errorsController;
          }
  
          public void setErrorsController(ErrorsController errorsController) {
              this.errorsController = errorsController;
          }
  
          public ResultStatus getResultStatus() {
              return resultStatus;
          }
  
          public void setResultStatus(ResultStatus resultStatus) {
              this.resultStatus = resultStatus;
          }
  
          public TBUSER_INFO getUserInfo() {
              return userInfo;
          }
  
          public void setUserInfo(TBUSER_INFO userInfo) {
              this.userInfo = userInfo;
          }
      }
  
  public class GetUserIdByUserNameOutput {
  
      private Long UserId;
      private ResultStatus resultStatus;
  
      public GetUserIdByUserNameOutput() {
  
      }
  
      public GetUserIdByUserNameOutput(Long userId, ResultStatus resultStatus) {
          super();
          UserId = userId;
          this.resultStatus = resultStatus;
      }
  
      public GetUserIdByUserNameOutput(ResultStatus resultStatus) {
          this.resultStatus = resultStatus;
      }
  
      public Long getUserId() {
          return UserId;
      }
  
      public void setUserId(Long userId) {
          UserId = userId;
      }
  
      public ResultStatus getResultStatus() {
          return resultStatus;
      }
  
      public void setResultStatus(ResultStatus resultStatus) {
          this.resultStatus = resultStatus;
      }
  }
  </code></pre>
  
  <p>If you look on this method :</p>
  
  <pre><code>public MsgStatus inputMapping() {
              MsgStatus msgStatus = MsgStatus.SUCCESS_MSG;
              this.setGetUserIdByUserNameOutput(new GetUserIdByUserNameOutput());
  </code></pre>
  
  <p>I start to create "new GetUserIdByUserNameOutput()" all the time with every request for the same API!! , and I guess is not efficient and heavy for the system for creating a lot of instance of GetUserIdByUserNameOutput after some times ...</p>