title: When and how to instantiate a Spring Bean in my Rest Api
body: <p>First of all, I'm a relative noob to Spring Boot, so keep that in mind.</p>
  
  <p>I've got a REST api in which I'm trying to minimize database calls for the same object and I've determined that using a Spring Bean scoped to the Request is what I want. Assuming that is correct, here is what I'm trying to do:</p>
  
  <p>1) Controller takes in a validated <code>PhotoImportCommandDto</code> command</p>
  
  <p><strong>PhotoCommandController</strong></p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST)
      public ResponseEntity&lt;?&gt; importPhoto(@Valid @RequestBody PhotoImportCommandDto command){
  ...
  }
  </code></pre>
  
  <p>2) <code>PhotoImportCommandDto</code> is validated. Note the custom <code>@UserExistsConstraint</code> which validates that the user exists in the database by calling a service method.</p>
  
  <p><strong>PhotoImportCommandDto</strong></p>
  
  <pre><code>@Component
  public class PhotoImportCommandDto extends BaseCommand {
  
      @NotNull(message = "userId must not be null!")
      @UserExistsConstraint
      private Long userId;
  ...
  }
  </code></pre>
  
  <p>What I would like to do is somehow set a Spring Bean of the user that is validated in the <code>@UserExistsConstraint</code> and reference it in various methods that might be called throughout a Http request, but I'm not really sure how to do that. Since I've never really created my own Spring Beans, I don't know how to proceed. I've read various guides like <a href="http://www.baeldung.com/spring-bean-scopes" rel="nofollow noreferrer">this</a>, but am still lost in how to implement it in my code.</p>
  
  <p>Any help/examples would be much appreciated.</p>