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
}
}