What are different types of Spring IOC container?
In Spring container or Spring IOC container is at the core of Spring framework. To learn about basics of IOC container click here.
Here we will learn about different types of IOC container provided by Spring framework.
Spring provides two types of containers as listed below:
-
Bean factories
-
Application context
About Bean Factories
Bean factories is interface provided by spring that helps in managing registry of different beans and their dependencies.
Bean factories is defined by org.springframework.beans.factory.BeanFactory interface.
We can use spring bean factory to:
-
read bean definitions from configuration metadata (eg, applicationContext.xml file) and
-
then access them using the bean definitions.
This can be done as shown below:
public static void main(String[] args) {
//read instructions from configuration metadata
Resource resource = new ClassPathResource("applicationContext.xml");
//create BeanFactory
BeanFactory factory = new XmlBeanFactory(resource);
//Get bean with name SampleExampleBean defined in xml file
SampleExample example = (SampleExample) factory.getBean("SampleExampleBean");
example.printTest();
}
You can read the complete demo application of the above example using bean factory here.
Clicking on same link you can understand other way of creating BeanFactory as well.
About Application Context
ApplicationContext container is built on top of BeanFactory container and thus contains additional functionalities (such as ability to resolve textual messages from a property files and the ability to publish application events to interested event listeners) while including all the functionalities/capabilities of BeanFactory.
Thus in most of the case while choosing appropriate container for spring application, Application Context enjoys upper hand over BeanFactory.
However, its possible to work with spring using either of them.
There are different types of application contexts available in spring. You can get all the details here in spring tutorial.
Like BeanFactory, we can use spring ApplicationContext to:
-
read bean definitions from configuration metadata (eg, applicationContext.xml file) and
-
then access them using the bean definitions.
This can be done in application context in much easier way as shown below:
public static void main(String[] args) {
//create application context and read metadata from xml file
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//Get bean with name SampleExampleBean defined in xml file
SampleExample example = (SampleExample) context.getBean("SampleExampleBean");
example.printTest();
}
You can see here:
-
You can see that we don't need to separately create resource by feeding in xml metadata file and then feed the resource in BeanFactory interface to create container. We just need one line of code and could access any bean as shown below:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
You can find the complete example to read bean definitions and access bean using application context here.
Thus we have covered both the spring containers or ioc container in this spring interview questions section.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Sonu Pandit
I am editor in chief at tutorialsinhand.com responsible for managing, reviewing and sending articles/contents for final approval to get published. Connect with me@https://www.linkedin.com/in/sonu-pandit-a77b471ab/. Join write4us program & share your skill
Page Views :
Published Date :
Jul 22,2020