Consider a case where a class annotated with Component has two constructors like this:
@Component
class MyClass {
@VisibleForTesting
MyClass(int someValue) {
// omitted
}
public MyClass() {
this(42);
}
}
Even though this works fine at runtime and the result of the correct no-args constructor is injected, this results in a finding of S6829 that one of the constructors should have the Autowired annotation.
However, I cannot annotate the public no-args constructor as I then get an inspection error: “Autowired constructor without parameters”.
Now I am wondering: should the sonar rule be “smarter” and recognize that he no-args constructor is correct without needing the Autowired annotation or is the IntelliJ warning incorrect here? Should I suppress the Sonar finding or the IntelliJ inspection?
I tried reproducing the issue from the IDE, but without success.
Can you please tell me which version of IntelliJ are you using?
Additionally, I couldn’t find any references that constructors without parameters shouldn’t be annotated with @Autowired.
Can you please provide me with some more details about this?
A screenshot could be useful.
The inspection error I get, when I remove the @Autowired annotation is: