title: Servlets should not have mutable instance fields false positive with Spring autowire (squid:S2226)
body: <p>I have a Servlet that have Spring auto-wiring capabilities using:</p>
  
  <pre><code>WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext())
                      .getAutowireCapableBeanFactory().autowireBean(this);
  </code></pre>
  
  <p>When I'm autowiring my beans SonarQube warns with `Servlets should not have mutable instance fields <a href="https://github.com/SonarSource/sonar-java/blob/master/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2226_java.html" rel="nofollow noreferrer">(squid:S2226)</a></p>
  
  <pre><code>@Autowired
  MyBean myBean;
  </code></pre>
  
  <p>Is it SonarQube bug which ignores Spring autowiring? Can I add different annotation to prevent this warning? am I missing something?</p>
  
  <blockquote>
    <p>By contract, a servlet container creates one instance of each servlet and then a dedicated thread is attached to each new incoming HTTP request to process the request. So all threads share the servlet instances and by extension their instance fields. To prevent any misunderstanding and unexpected behavior at runtime, all servlet fields should then be either static and/or final, or simply removed.</p>
  </blockquote>
  
  <p><strong>EDIT</strong></p>
  
  <p>I found similar <a href="https://jira.sonarsource.com/browse/SONARJAVA-1550?jql=text%20~%20%22S2226%22" rel="nofollow noreferrer">issue</a> that was fixed about Java's <code>@Inject</code> that should not raise this warning </p>
  
  <blockquote>
    <p>In practice when a field is annotated with @Inject it won't be mutated.
    So fields annotated with this annotation should not raise issue RSPEC-2226. </p>
  </blockquote>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Open a potential false positive issue in <a href="https://community.sonarsource.com/t/s2226-should-not-raise-issues-for-field-annotated-with-autowire-in-servlet/2627" rel="nofollow noreferrer">SonarSource Community</a></p>