Inspectopedia 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.


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

By default bundled with:

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin:

Spring, 241.16690

Last modified: 29 April 2024