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:
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.
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.
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.
It returns the different names for the bean name given.
This method returns the bean instance that is the same as the given object type if any.
This method returns an instance that may be shared or unshared of the bean which is specified.
It returns an occurrence of the specified bean, which can be either shared or independent.
This method returns an instance that may be shared or unshared of the bean which is specified.
It returns a sample of the specified bean, which may be shared or independent.
This method returns a provider for the specified bean that allows lazy on-demand instances to be retrieved that includes availability and uniqueness options.
This method returns a provider for the specified bean that allows lazy on-demand instances to be retrieved that includes availability and uniqueness options.
This method checks the type of the bean with the given name.
This method checks the type of the bean with the given name.
It determines if the bean is a prototype or not.
It determines if the bean is a shared singleton or not.
It is used to find out if the bean with the given name matches the specified type.
It checks if the bean with the given name is the same as the specified type.
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:
Now we will go to the various ApplicationContext Methods that come in the Inversion of Control (IoC) container of the Spring Applications:
This method is used to return the name for the deployed application to which this context belongs.
It exposes AutowireCapableBeanFactory functionality for this context.
It returns a favorable name for this context.
It returns a sole id of this application context.
This method returns the parent context, or null (in case of no parent). It is also a backbone for the context hierarchy.
It returns the timestamp when the context is loaded for the first time.
There are three main ways to configure the IoC container in the Spring Application:
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>