spring-bootjava-8lombokconstructor-injection

@AllArgsConstructor and Constructor Injection with Spring: is private final needed?


Let's say I have the following constructor injection (not Autowiring):

@Service
public class FooService {

    private final OrderService orderService;

    public FooService(OrderService orderService) {
        this.orderService = orderService;
    }
}

That can be replaced with:

@Service
@AllArgsConstructor
public class FooService {

    private final OrderService orderService;

}

Do I need to declare this as private and final to inject this service? Does Lombok take care of this like they do with @Data and beans? Any side-effects?


Solution

  • According to documentation

    @AllArgsConstructor generates a constructor with 1 parameter for each field in your class. Fields marked with @NonNull result in null checks on those parameters.

    So, no, it does not make your fields private & final as for example @Value annotation.