I have a piece of code with field injections I am trying to convert to use constructor injections instead. The initial code looks like this:
@Autowired
private Environment env;
@Autowired
private YYYAdaptor yyyAdaptor;
@Autowired
private JAXBContext jaxbContext;
And this is how I rewrite it:
private Environment env;
private YYYAdaptor yyyAdaptor;
private JAXBContext jaxbContext;
@Autowired
public YYYResource(Environment env, YYYAdaptor yyyAdaptor,
@Qualifier("YYYYReq") JAXBContext jaxbContext) {
this.env = env;
this.yyyAdaptor = yyyAdaptor;
this.jaxbContext = jaxbContext;
}
Doing this gives me a critical vulnerability on the sonar scan, with "this member" referring to each of the declared variables:
Annotate this member with "@Autowired", "@Resource", "@Inject", or "@Value", or remove it
What is the best way I can avoid using field injections while avoiding sonar throwing a fit?