Chapter 3. Containers

A Spring container is the backbone of the framework. A container is basically a pool of beans created in a memory space by the framework when the application starts up. An API provided by the framework exposes methods to query these beans from the container.

We’ll start the chapter by looking at the containers and different categories. We’ll also look at details about concepts such as AutoWiring.

Containers

Understanding containers is an important task when working with the Spring Framework as they are crucial moving pieces in a Spring’s jig-saw puzzle.

During the startup process, the beans are instantiated, the associations and relationships are created, all the relationships are satisfied, and dependencies are injected. All named beans are available for querying using an API. Some beans are lazily loaded while others are loaded during the initialization of the container. This means that unless the bean is requested by our application or some other bean (as a part of dependency), the framework will not instantiate the bean, except loading singletons.

Spring containers primarily fall into two categories: Bean Factories and Application Contexts—supported by the BeanFactory and ApplicationContext classes respectively. The names are sort of misnomers, as they do not give any clue as to what these containers are and what they do.

The BeanFactory is a simple container supporting basic dependency injection, whereas ApplicationContext is an extension of BeanFactory, which ...

Get Just Spring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.