Defining eager and lazy spring beans
At this point, it is clear already how beans are instantiated inside Spring 5.0 containers. The practical definition of Inversion of Control and Dependency Injection design patterns has been established too. Two approaches to implementing a container have been illustrated with the previous recipes. This time we will provide a recipe on how to decide what form of instantiation the beans must undergo in a container.
Getting started
We need both ch02-xml
and ch02-jc
in this recipe since the bean loading strategy depends on what type of ApplicationContext
container is being used. There are two bean loading strategies in the Spring 5.0 framework namely eager and lazy loading.
How to do it...
Let us illustrate the eager and lazy loading of beans in a context definition using these steps:
- In the case of the XML-based
ApplicationContext
, eager loading means all the beans in the definition will be loaded and initialized aggressively in the heap memory during start...