Inspectopedia 2024.3 Help

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.

Settings or Preferences | Editor | Inspections | Spring | Spring Core | Code

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.3, Qodana for JVM 2024.3,

Can be installed with plugin:

Spring, 243.23126

Last modified: 03 December 2024