title: How to destroy or clear Bean object state in every use in spring boot
body: <p>I'm new in spring boot and trying to figure out when we create a bean using @Bean and tries to access that bean where ever it requires using @Autowired. But i know @Bean is by default singleton and it will save its state but i want to clear its state so that it will give new newly append data or null if no data was append. Kindly help me on this. And i also want to know that is i'm following correct coding standard by using Bean with autowired because i want my every api give similar type of response that's why i create a pojo and make it a bean so that i don't have to create object again and again. Sorry if my problem is silly.. thanks in advance </p>
  
  <p>This is my main class</p>
  
  <pre><code>@SpringBootApplication
  public class GyftiV2Application {
  
      public static void main(String[] args) {
          SpringApplication.run(GyftiV2Application.class, args);
      }
      @Bean
      public ResponseData getResponse() {
          return new ResponseData();
      }
  }
  </code></pre>
  
  <p>Below is the pojo</p>
  
  <pre><code>public class ResponseData {
  
      private boolean responce;
      private String error;
      private List&lt;?&gt; data = new ArrayList&lt;&gt;();
  
      public ResponseData() {
      }
  
      public boolean isResponce() {
          return responce;
      }
  
      public void setResponce(boolean responce) {
          this.responce = responce;
      }
  
      public String getError() {
          return error;
      }
  
      public void setError(String error) {
          this.error = error;
      }
  
      public List&lt;?&gt; getData() {
          return data;
      }
  
      public void setData(List&lt;?&gt; data) {
          this.data = data;
      }
  }
  </code></pre>
  
  <p>Below is the service where is used my bean </p>
  
  <pre><code>@Service
  public class UserServiceImpl implements UserService {
  
  @Autowired
  private ResponseData resData;
  
  @Autowired
  private UserRepository userRepository;
  
  public ResponseData changePassword(PasswordChange pass) {
      User user = userRepository.getOne(pass.getUserId());
      if (null != user) {
          if (pass.getOldPassword().equals(user.getUser_password())) {
              if ((pass.getNewPassword().trim()).equals(pass.getConfirmPassword().trim())) {
                  user.setUser_password(pass.getNewPassword());
                  userRepository.save(user);
                  resData.setResponce(true);
                  resData.setData(Collections.singletonList("Password change successfully"));
                  return resData;
              } else {
                  resData.setResponce(false);
                  resData.setError("Please write the same new password in the confirm section");
                  return resData;
              }
          } else {
              resData.setResponce(false);
              resData.setError("Please write the correct old password");
              return resData;
          }
      } else {
          resData.setResponce(false);
          resData.setError("Something went wrong userId is not correct");
          return resData;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>With Controller </p>
  
  <pre><code>@RestController
  @RequestMapping("/user")
  public class UserController {
  
  @Autowired
  private UserService userService;
  
  @PostMapping(value = "/changePassword")
      public ResponseEntity&lt;ResponseData&gt; changePassword(@RequestBody PasswordChange pass) {
          ResponseData response = userService.changePassword(pass);
          if (response.isResponce()) {
              return new ResponseEntity&lt;&gt;(response, HttpStatus.OK);
          }
          return new ResponseEntity&lt;&gt;(response, HttpStatus.BAD_REQUEST);
      }
  
  }
  </code></pre>
  
  <p>But when i didn't pass same newPassoword and confirmPassword then i got the response </p>
  
  <pre><code>{
    "responce": false,
    "error": "Please write the same new password in the confirm section",
    "data": []
  }
  </code></pre>
  
  <p>And when i pass everything correct then i got the response </p>
  
  <pre><code>{
    "responce": true,
    "error": "Please write the same new password in the confirm section",
    "data": [
      "Password change successfully"
    ]
  }
  </code></pre>
  
  <p>You will clearly see resData save its state that's why error key is still exist. I know if i pass "" in error field it will fix this issue but is there is any why to clear @Bean state ? Thanks for the help.</p>