liveBook · Manning

Eager instantiation

As explained in Spring Start Here, Second Edition

Eager instantiation is the creation of an object or bean before it is explicitly requested, typically during container initialization.

Eager instantiation is Spring’s default behavior for singleton beans: Spring creates them while initializing the context, even if the application does not request them. section 5.1.2

A bean created before it is requested

A CommentService constructor prints a message when called. Start the Spring context without retrieving CommentService, and the message still appears: Spring created the bean during initialization. section 5.1.2

Listing 5.8 Adding a log line to the bean’s constructor
@Service
public class CommentService {
 
  public CommentService() {
    System.out.println("CommentService instance created!");
  }
}

The constructor log makes bean creation observable. section 5.1.2

How Spring Start Here, Second Edition covers it

How deep it goes
Defines eager instantiation and demonstrates its timing, without detailing container internals.
Examples
Uses a CommentService constructor log to show creation before the bean is retrieved.
What you can do after
Evaluate whether a bean is created during context initialization by observing its constructor output.
How it connects
Connects singleton scope and Spring context initialization with lazy instantiation as the contrast.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.