When I try to use the Resource annotation in a servlet, Sonar triggers rule squid:S2226 "Servlets should not have mutable instance fields" and tells me make the variable final or static.
But resource injection does not work and final and static variables.
Is it a bug in sonar or resource injection is not recommended anymore in servlets ?
public class MyServlet extends HttpServlet {
@Resource(name = "jdbc/database")
private DataSource dataSource;
}
A similar conundrum appears with ServletConfig
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
Here, config
cannot be made final, but making it static trigger the other rule squid:S2696 : "Instance methods should not write to "static" fields"
I meet this situation with SonarLint for Eclipse 3.3.1.201712071600, if that is useful.