Spring recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
Field injection/NullPointerException example:
class MyComponent {
@Inject MyCollaborator collaborator;
public void myBusinessMethod() {
collaborator.doSomething(); // -> NullPointerException
}
}
Constructor injection should be used:
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(); // -> safe
}
}