One of the most important topic when we start learning Spring is Spring IoC container. We need to understand what a Spring IoC Container is, and how does it work, to understand how Spring works. We’ll understand this by answering few important questions about Spring IoC container.
What is Spring IoC Container?
Spring IoC container is responsible for instantiating, configuring and assembling the beans. It is the Spring IoC container which does the core of the job. Spring IoC container is responsible of implementing Dependency Injection and Inversion of Control (IoC) principles on which Spring’s popularity is based.
Spring IoC Container is responsible for managing beans. Beans is just an object used in application but in Spring’s context when managed by container is called a bean.
The org.springframework.beans
and org.springframework.context
packages are the basis for Spring Framework’s IoC container.
The BeanFactory
interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext
is a sub-interface of BeanFactory
.
In short, the BeanFactory
provides the configuration framework and basic functionality, and the ApplicationContext
adds more enterprise-specific functionality.
How is Spring IoC container represented in Spring?
Spring IoC container is represented by org.springframework.context.ApplicationContext
interface. ApplicationContext
is a sub-interface of BeanFactory
interface.
This is read-only while the application is running. This interface provides:
- Bean factory methods for accessing application components.
- The ability to publish events to registered listeners.
- Message resource handling used in internationalization.
- Application specific contexts such as the
WebApplicationContext
for use in web applications.
BeanFactory
provides the basic functionality whereas the ApplicationContext
provides many more functionalities used in enterprise applications. Following are the known implementing classes of ApplicationContext
:
- AbstractApplicationContext
- AbstractRefreshableWebApplicationContext
- AnnotationConfigWebApplicationContext
- GenericApplicationContext
- GenericXmlApplicationContext
- StaticApplicationContext
- AbstractRefreshableConfigApplicationContext
- FileSystemXmlApplicationContext
- ResourceAdapterApplicationContext
- AbstractRefreshableApplicationContext
- AbstractXmlApplicationContext
- ClassPathXmlApplicationContext
- GenericGroovyApplicationContext
- GroovyWebApplicationContext
- StaticWebApplicationContext
- AnnotationConfigApplicationContext
- GenericWebApplicationContext
- XmlWebApplicationContext
In stand-alone applications, it is common to create an instance of AnnotationConfigApplicationContext
or ClassPathXmlApplicationContext
.
How does Spring IoC container work?
The instructions can be provided to the container in either of these formats:
- XML
- Java annotations
- Java configuration code
- Groovy scripts
The configuration metadata represents the instructions to the Spring container to instantiate, configure, and assemble the components in your application. The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written.
Spring configuration includes at least one, and usually multiple, bean definitions that the container manages. In Java configuration, this is typically done with methods annotated with @Bean
within a @Configuration
class, where each method represents a bean definition.
The container does the following in short:
Container reads the business objects and convert into beans to be used in the execution of application by applying the configuration metadata.
A Java application is usually made up objects communicating with each other. An object may have a dependency on another object. These objects may not work alone. We define the dependencies in XML, Java annotations or Java configuration code. The container reads these instructions and produces the beans which then can be used in applications. This is the core function of Spring IoC container.