title: (Spring) Can I use dependency injection inside a loop?
body: <p>I am currently working on a Spring Boot application that allows users to save categories into a database. I can get my code to "work", however, I think it limits the amount of testing I can do, hence my question.</p>
  
  <p>The controller receives a list of categories. The controller iterates over these categories, validates them and depending on whether they are valid, they are saved to a database. The controller finally returns a list of messages, such that the recipient can identify which category has been accepted or rejected, etc.  </p>
  
  <p>I have a list of model messages (List), which upon each iteration, the controller instantiates a new model message (new ModelMessage()) and eventually adds it to the List. <strong>Is there a way to inject a new ModelMessage upon each iteration or do I need to use the new keyword</strong>? If I do use the new keyword, I feel like this is limiting my testability/tightly coupling my controller to the model message. </p>
  
  <p>The controller:</p>
  
  <pre><code>@PostMapping("/category")
      public String saveCategoryModelToDatabase(@RequestBody CategoryModelWrapper categoryModelWrapper){
          List&lt;CategoryModel&gt; categoryModelList = categoryModelWrapper.getCategoryModelList();
          modelMessageList.clear();
  
          for(CategoryModel categoryModel : categoryModelList){
              //Resetting model
              modelMessage = new ModelMessage(); //This tightly couples my method to the ModelMessage class, which is bad for testing?
              //@Autowired modelMessage; &lt;-- something like this? Inject a new ModelMessage with each iteration.
              modelMessage.setName(categoryModel.getName());
  
              //Resetting categoryModelErrors
              Errors categoryModelErrors = new BeanPropertyBindingResult(categoryModel, "categoryModel");
              categoryModelValidator.validate(categoryModel, categoryModelErrors);
  
  
              if(categoryModelErrors.hasErrors()){
                  modelMessage.setStatus(ModelMessageStatusEnum.REJECTED);
                  modelMessage.setReason(MODEL_MESSAGE_0004);
              }
  
              if(categoryModelService.save(categoryModel)){
                  modelMessage.setStatus(ModelMessageStatusEnum.ACCEPTED);
              }
              else{
                  modelMessage.setStatus(ModelMessageStatusEnum.REJECTED);
                  modelMessage.setReason(MODEL_MESSAGE_0005);
              }
              modelMessageList.add(modelMessage);
          }
  
          return gson.toJson(modelMessageList);
      }
  </code></pre>
  
  <p>An example of the response to the recipient:</p>
  
  <pre><code>[{"name":"Arts","status":"ACCEPTED"},{"name":"Business","status":"ACCEPTED"},{"name":"Gaming","status":"ACCEPTED"},{"name":"Deals","status":"REJECTED","reason":"Category rejected because of an unexpected exception, i.e. possibly due to duplicate keys."}]
  </code></pre>
  
  <p>Thanks for any help :)</p>