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
}
}
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
SpringJavaAutowiredFieldsWarningInspection- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Spring, 243.23126 |
Last modified: 03 December 2024