Spring IOC Container
Spring IoC Container is a core part of the Spring framework which is used to manage the application bean. It injects dependencies when a bean is created and manages the bean life cycle during execution.
The fundamental tasks of Spring IoC are:
-
Instantiating
-
Configuring, and
-
Assembling Bean
The IOC container gets configuration related information from the Spring configuration file. That can be either XML or Java files.
The container uses dependency injection (DI) to manage the components that make up an application.
Spring provides two types of IOC containers:
-
BeanFactory
-
Application Context
Spring BeanFactory Interface
It is an IoC container that is responsible for maintaining beans and their dependencies. It is basically an interface that provides basic functionalities.
BeanFactory Interface Methods:
Following are the BeanFactory Interface methods:
Method Name |
Description |
---|---|
|
It checks whether this bean factory contains a bean definition or externally registered singleton instance with the given name. |
|
It returns the aliases for the given bean name if any. |
|
It returns the bean instance that uniquely matches the given object type if any. |
|
It returns an instance, which may be shared or independent, of the specified bean. |
|
It returns an instance, which may be shared or independent, of the specified bean. |
|
It returns an instance, which may be shared or independent, of the specified bean. |
|
It returns an instance, which may be shared or independent, of the specified bean. |
|
It returns a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. |
|
It returns a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. |
|
It determines the type of the bean with the given name. |
|
It determines the type of the bean with the given name. |
|
It checks whether this bean a prototype. |
|
It checks whether this bean a shared singleton. |
|
It checks whether the bean with the given name matches the specified type. |
|
It checks whether the bean with the given name matches the specified type. |
Spring ApplicationContext Sub-Interface
The ApplicationContext
is a sub-interface of BeanFactory
and provides more enterprise like functionality. It adds Application-layer specific contexts such as the WebApplicationContext
for web applications.
There are several implementations for this ApplicationContext interface such as:
-
ClassPathXmlApplicationContext
-
XmlWebApplicationContext
-
FileSystemXmlApplicationContext
ApplicationContext Methods:
The following are the methods in ApplicationContext Interface.
Method Name |
Description |
---|---|
|
It returns a name for the deployed application that this context belongs to. |
|
It exposes AutowireCapableBeanFactory functionality for this context. |
|
It returns a friendly name for this context. |
|
It returns the unique id of this application context. |
|
It returns the parent context, or null if there is no parent and this is the root of the context hierarchy. |
|
It returns the timestamp when this context was first loaded. |
Difference Between BeanFactory and ApplicationContext
Both the interfaces(BeansFactory and ApplicationsContext) acts as the IoC container. The BeanFactory interface is a base interface and provides all the basic functionalities to create and run the IoC container while the ApplicationContext interface is a subinterface of the BeanFactory interface that adds some extra functionalities like simple integration with Spring's AOP, message resource handling (for I18N), application layer specific context, etc. So, we can use ApplicationContext for better features.
How to Configure the IoC Container?
This is the basic structure of XML-based configuration metadata.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
There are mainly three ways by which we can configure our IoC container.
-
XML Based
-
Annotation Based
-
Java-Based
In our next section, we will discuss to configure the IoC container in all the possible ways.