The recommended way of injecting Spring Bean dependencies is to use constructor binding instead of field injection with @Autowired annotation.
With Lombok annotations constructors can be automatically generated so handling it this way is easy.
SonarQube explains it as:
"Dependency injection frameworks such as Spring, Quarkus, and others support dependency injection by using annotations such as @Inject and @Autowired. These annotations can be used to inject beans via constructor, setter, and field injection.
Generally speaking, field injection is discouraged. It allows the creation of objects in an invalid state and makes testing more difficult. The dependencies are not explicit when instantiating a class that uses field injection.
In addition, field injection is not compatible with final fields. Keeping dependencies immutable where possible makes the code easier to understand, easing development and maintenance.
Finally, because values are injected into fields after the object has been constructed, they cannot be used to initialize other non-injected fields inline."
The recommended way of injecting Spring Bean dependencies is to use constructor binding instead of field injection with
@Autowiredannotation.With Lombok annotations constructors can be automatically generated so handling it this way is easy.
SonarQube explains it as:
"Dependency injection frameworks such as Spring, Quarkus, and others support dependency injection by using annotations such as @Inject and @Autowired. These annotations can be used to inject beans via constructor, setter, and field injection.
Generally speaking, field injection is discouraged. It allows the creation of objects in an invalid state and makes testing more difficult. The dependencies are not explicit when instantiating a class that uses field injection.
In addition, field injection is not compatible with final fields. Keeping dependencies immutable where possible makes the code easier to understand, easing development and maintenance.
Finally, because values are injected into fields after the object has been constructed, they cannot be used to initialize other non-injected fields inline."