Given a class AnimalService:
public class AnimalService{
private DogService dogService;
private AnimalService(@Lazy DogService dogService){
this.dogService = dogService;
}
}
}
In this case if I want to use Lombok annotations is there a way to keep the @Lazy loading?
The following code will do the same as the above code?
@AllArgsConstructor
public class AnimalService{
@Lazy
private DogService dogService;
}
@Lazy
public class DogService{
//code
}
Is this an appropriate way to use @Lazy annotation with Lombok?
It won't work out of the box, but you can configure Lombok to copy the @Lazy
annotation from the field to the constructor's parameter.
lombok.config
lombok.copyableAnnotations += org.springframework.context.annotation.Lazy
The lombok.config should be placed in the project's root or src folder.
If you want it only for a single class without doing a global Lombok config you can use the following snippet:
@AllArgsConstructor(onConstructor = @__(@Lazy))
public class AnimalService{
@Lazy
private DogService dogService;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With