logo

Inversion Of Control (IoC) Container Of Spring


Show

Inversion of Control (IoC) Container is present at the core of the Spring framework. It is also called dependency injection (DI). It is a framework for implementing automatic dependency injection. IoC is used to manage the complete life cycle of beans. This container is used to create the objects, bind them together, configure them right from the beginning till the end.

The IoC container retrieves the information as specified in the XML file and completes the task as defined in the application. IoC containers are of two types:

  • BeanFactory
  • ApplicationContext

Difference between BeanFactory and the ApplicationContext

  • The BeanFactory of the IoC container is defined by the org.springframework.beans.factory.BeanFactory interface whereas ApplicationContext is defined by the org.springframework.context.ApplicationContext interface.
  • The difference between the BeanFactory and the ApplicationContext is that the ApplicationContext has an extra layer of functionality than the BeanFactory. It simplifies integration with Spring's Aspect-oriented programming (AOP), the ability to resolve textual messages from a properties file, and provides application-layer specific context for web applications with the functionality of (e.g. WebApplicationContext).
  • The ApplicationContext container is considered a better container than the BeanFactory because it has all functionality of the BeanFactorycontainer
  • The BeanFactory container of the IoC is generally used for lightweight applications like mobile devices or applet-based applications on the other hand the ApplicationContext is more used for complex applications such as web applications.

How to use BeanFactory?

The XmlBeanFactory is the implementation class for the interface of the BeanFactory container of the Inversion of Control (IoC)of the Spring framework. To use the BeanFactory, we are required to create the instance of XmlBeanFactory class as mentioned below:

Resource resource=new ClassPathResource("applicationContext.xml");  
BeanFactory factory=new XmlBeanFactory(resource);  

The XmlBeanFactory class constructor retrieves the Resource object which is required to create the object of BeanFactory.

How to use ApplicationContext?

The implementation class of ApplicationContext interface is ClassPathXmlApplicationContext. We are required to implement ClassPathXmlApplicationContext class to use the ApplicationContext as given below:

ApplicationContext context =   new ClassPathXmlApplicationContext("applicationContext.xml");  

ClassPathXmlApplicationContext class constructor receives a string, so we can provide the name of the XML file to create an ApplicationContext instance.

Spring BeanFactory Interface Methods:

  • boolean containsBean(String name)

This method is used to find if the bean factory contains a bean definition or it has a registered singleton instance externally with the given name.

  • String[] getAliases(String name)

It returns the different names for the bean name given.

  • T getBean(Class requiredType)

This method returns the bean instance that is the same as the given object type if any.

  • T getBean(Class requiredType, Object... args)

This method returns an instance that may be shared or unshared of the bean which is specified.

  • Object getBean(String name)

It returns an occurrence of the specified bean, which can be either shared or independent.

  • T getBean(String name, Class requiredType)

This method returns an instance that may be shared or unshared of the bean which is specified.

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

It returns a sample of the specified bean, which may be shared or independent.

  • ObjectProvider getBeanProvider(Class requiredType)

This method returns a provider for the specified bean that allows lazy on-demand instances to be retrieved that includes availability and uniqueness options.

  • ObjectProvider getBeanProvider(ResolvableType requiredType)

This method returns a provider for the specified bean that allows lazy on-demand instances to be retrieved that includes availability and uniqueness options.

  • Class<?> getType(String name)

This method checks the type of the bean with the given name.

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

This method checks the type of the bean with the given name.

  • boolean isPrototype(String name)

It determines if the bean is a prototype or not.

  • boolean isSingleton(String name)

It determines if the bean is a shared singleton or not.

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

It is used to find out if the bean with the given name matches the specified type.

  • boolean isTypeMatch(String name, ResolvableType typeToMatch)

It checks if the bean with the given name is the same as the specified type.

Spring ApplicationContext Methods:

Before knowing the methods of the ApplicationContext, we came to know that the ApplicationContext is a sub-interface of the BeanFactory container of the Inversion of Control (IoC) that provides more powerful functionality than its BeanFactory counterpart.

The ApplicationContext interface can be implemented by the following:

  • ClassPathXmlApplicationContext
  • XmlWebApplicationContext
  • FileSystemXmlApplicationContext

Now we will go to the various ApplicationContext Methods that come in the Inversion of Control (IoC) container of the Spring Applications:

  • String getApplicationName()

This method is used to return the name for the deployed application to which this context belongs.

  • AutowireCapableBeanFactory getAutowireCapableBeanFactory()

It exposes AutowireCapableBeanFactory functionality for this context.

  • String getDisplayName()

It returns a favorable name for this context.

  • String getId()

It returns a sole id of this application context.

  • ApplicationContext getParent()

This method returns the parent context, or null (in case of no parent). It is also a backbone for the context hierarchy.

  • long getStartupDate()

It returns the timestamp when the context is loaded for the first time.

Configuration of Inversion of Control (IoC) Container

There are three main ways to configure the IoC container in the Spring Application:

  • XML Based
  • Annotation Based
  • Java-Based

Here, we will give a coded view structure of XML-based configuration metadata:


<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="...">  
        
    </bean>
    
</beans>