Uses `@ Lazy` in a component constructor equal to the annotation of each argument?

In Spring, consider the @Service class, which has the following @Service constructor:

 public DogService(@Lazy CatService catService, @Lazy MouseService mouseService) { this.catService = catService; this.mouseService = mouseService; } 

equivalent to?

 @Lazy public DogService(CatService catService, MouseService mouseService) { this.catService = catService; this.mouseService = mouseService; } 
+5
source share
1 answer

Yes, this is equivalent.

@Lazy javadoc states:

In addition to its role for component initialization, this annotation can also be placed on injection points marked with org.springframework.beans.factory.annotation.Autowired or javax.inject.Inject : In this context, this creates a lazy permission proxy for all affected dependencies, alternatively prior to using org.springframework.beans.factory.ObjectFactory or javax.inject.Provider .

Main part:

this leads to the creation of a lazy resolution proxy for all affected Dependencies

in terms of dependencies, your DogService bean has two of them anyway: * t26> and MouseService mouseService .
Thus, annotating the constructor or all parameters individually will lead to the same result: two dependencies will be lazy loaded.

Note. I tested them, and in both cases the behavior is the same.

+1
source

Source: https://habr.com/ru/post/1276025/


All Articles