Field injection seems like a tidy way to get your classes what they need to do their jobs, but it’s really a NullPointerException
waiting to happen unless all your class constructors are private
. That’s because any class instances that are constructed by callers,
rather than instantiated by a Dependency Injection framework compliant with the JSR-330 (Spring, Guice, …), won’t have the ability to perform the
field injection.
Instead @Inject
should be moved to the constructor and the fields required as constructor parameters.
This rule raises an issue when classes with non-private
constructors (including the default constructor) use field injection.
Noncompliant code example
class MyComponent { // Anyone can call the default constructor
@Inject MyCollaborator collaborator; // Noncompliant
public void myBusinessMethod() {
collaborator.doSomething(); // this will fail in classes new-ed by a caller
}
}
Compliant solution
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();
}
}