The Spring Container
The Spring framework is one of the most popular extensions to the Java
language. Although the Spring library has spread into numerous domains,
the heart of the library is a dependency injection container. The Spring container
is responsible for instantiating, configuring, and injecting components (called
beans) into a running application.
Spring Configuration
The Spring container requires configuration that defines beans and their
relationship within an application. Configuration can be provided in the form
of XML, annotations, or java code. XML configuration is commonly used due
to its intuitive nature, but it’s susceptible to refactoring errors because the names
of classes and packages often change. Annotations provide an alternative
to XML, but the resulting configuration is decentralized and any changes require
modifications to the source code. Java configuration files combine the
centralized configuration of XML with the convenience of annotations, but it
lacks some of the flexibility that XML provides.
Spring Lifecycle
The Spring container begins by instantiating an ApplicationContext with
one of the available configuration options. The container instantiates all of the
beans and injects their dependencies via constructor injection, setter injection,
or reflection. Finally, the container invokes callback methods on beans that hook
into the lifecycle of the container. For example, the container will invoke the
afterPropertiesSet()
method on any bean that implements the
InitializingBean
interface.
Questions
What is the inversion of control pattern?
What is the difference between a service locator and dependency injection?
What is the difference between constructor injection and setter injection?
What is the Spring container?
What are the different ways of configuring a Spring container?
What is the lifecycle of a Spring container?
|