title: How to have thread safe controller in spring boot
body: <p>How should I create a controller which is thread safe?</p>
  
  <p>As per the best practice controllers are singleton.</p>
  
  <p>Consider the below code in which im storing user data through a autowired service Object ,which makes my code stateful.
  How would i make the below code thread safe.</p>
  
  <pre><code>@RestController
  class ApiController {
  
      @Autowired
      IDbService&lt; User &gt; iDBService;
  
      @RequestMapping(value = "/api/adduser", method = RequestMethod.POST)
      public ResponseEntity&lt;User&gt; createUser(@RequestBody User user){
  
          User savedUser=iDBService.create(user);
  
          return new ResponseEntity&lt;User&gt;(savedUser, HttpStatus.CREATED);
      }
  </code></pre>
  
  <p>Here is my Service implementation.
  I have shared variable in my service</p>
  
  <pre><code>public class IDbServiceImpl&lt;T&gt; implements IDBService&lt;T&gt;{
  
  @Autowired
  GenericRepository&lt;T, Serializable&gt; genericRepository;
  
  @Override
  public T create(T object) {
      return  genericRepository.save(object);
  }
  </code></pre>
  
  <p>}</p>