Non recommended 'field' injections
Reports injected or autowired fields in Spring components.
The quick-fix suggests the recommended constructor-based dependency injection in beans and assertions for mandatory fields.
Example:
class MyComponent {
@Inject MyCollaborator collaborator; // injected field
public void myBusinessMethod() {
collaborator.doSomething(); // throws NullPointerException
}
}
After applying the quick-fix:
class MyComponent {
private final MyCollaborator collaborator;
@Inject
public MyComponent(MyCollaborator collaborator) {
Assert.notNull(collaborator, "MyCollaborator must not be null!");
this.collaborator = collaborator;
}
public void myBusinessMethod() {
collaborator.doSomething(); // now this call is safe
}
}
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Spring, 233.SNAPSHOT |
Last modified: 13 July 2023