At the heart of Spring is its lightweight Inversion of Control (IoC) container functionality. One or more instances of a container are used to configure and wire together application and framework objects, and manage their lifecycles. The key principles of IoC ensure that the great majority of these objects do not have to have dependencies on the container itself, and can generally deal with related objects purely in terms of (Java) interfaces or abstract superclasses, without having to worry about how those other objects are implemented, or how to locate them. The IoC container is the basis for many of the features that will be introduced in the chapters that follow.
In this chapter, you will learn about the configuration and usage of Spring's bean factories and application contexts, as the embodiment of Spring's IoC container functionality. You will learn about the BeanFactory and ApplicationContext interfaces, along with related interfaces and classes, used when the container needs to be created or accessed in a programmatic fashion. This chapter will focus on the BeanFactory and ApplicationContext variants configured in a declarative fashion via XML. These generally have the most built-in functionality in terms of configuration and usage, and are the ones used by Spring users in the vast majority of cases. Note however that Spring decouples container configuration and usage. The next chapter will show how you may also access the full power of the containers using programmatic mechanisms for configuration, as well as alternative declarative formats.
We'll be looking at:
Inversion of control and Dependency Injection
Basic object configuration and wiring of objects in the Spring container
How dependencies are resolved, and explicit versus automatic wiring of dependencies
Handling object lifecycles in the Spring container
Abstracting access to services and resources
Bean and bean factory post-processors for customizing bean and container behavior
Programmatic usage of the BeanFactory and ApplicationContext interfaces