A Spring bean is normally created when the application context starts, even if nothing uses it. @Lazy defers creation until the bean is requested. section 5.1.2
Listing 5.11 Configuring lazy instantiation
@Service
@Lazy #A
public class CommentService {
public CommentService() {
System.out.println("CommentService instance created!");
}
}Place @Lazy on a stereotype-annotated class to defer its bean’s creation; the annotation can also go on a @Bean method. section 5.1.2
When creation happens
A lazy CommentService produces no constructor output at startup if unused. Retrieving it from the context triggers creation. Eager instantiation is generally more convenient: it avoids checks when beans delegate to one another and exposes creation problems at startup. section 5.1.2
