Signup/Sign In
LAST UPDATED: DECEMBER 29, 2020

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

boolean containsBean(String name)

It checks whether this bean factory contains a bean definition or externally registered singleton instance with the given name.

String[] getAliases(String name)

It returns the aliases for the given bean name if any.

<T> T getBean(Class<T> requiredType)

It returns the bean instance that uniquely matches the given object type if any.

<T> T getBean(Class<T> requiredType, Object... args)

It returns an instance, which may be shared or independent, of the specified bean.

Object getBean(String name)

It returns an instance, which may be shared or independent, of the specified bean.

<T> T getBean(String name, Class<T> requiredType)

It returns an instance, which may be shared or independent, of the specified bean.

Object getBean(String name, Object... args)

It returns an instance, which may be shared or independent, of the specified bean.

<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType)

It returns a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.

<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType)

It returns a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.

Class<?> getType(String name)

It determines the type of the bean with the given name.

Class<?> getType(String name, boolean allowFactoryBeanInit)

It determines the type of the bean with the given name.

boolean isPrototype(String name)

It checks whether this bean a prototype.

boolean isSingleton(String name)

It checks whether this bean a shared singleton.

boolean isTypeMatch(String name, Class<?> typeToMatch)

It checks whether the bean with the given name matches the specified type.

boolean isTypeMatch(String name, ResolvableType typeToMatch)

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

String getApplicationName()

It returns a name for the deployed application that this context belongs to.

AutowireCapableBeanFactory getAutowireCapableBeanFactory()

It exposes AutowireCapableBeanFactory functionality for this context.

String getDisplayName()

It returns a friendly name for this context.

String getId()

It returns the unique id of this application context.

ApplicationContext getParent()

It returns the parent context, or null if there is no parent and this is the root of the context hierarchy.

long getStartupDate()

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.



About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.