Spring IoC Containers – Types of Spring Container
Job-ready Online Courses: Click for Success - Start Now!
1. Objective
In our last Spring Tutorial, we studied Spring Framework Architecture and today in this Spring Tutorial, you will learn about Spring IoC Containers and the work of IoC Container in development of Spring Applications. Also, you will be getting to know about the two distinct types of Spring IoC Containers which are a BeanFactory Container and ApplicationContext Container in Spring Framework.
So, let’s start Spring IoC Containers.
2. Spring IoC Containers
The Spring container is the core of Spring Framework. The container, use for creating the objects and configuring them. Also, Spring IoC Containers use for managing the complete lifecycle from creation to its destruction. It uses Dependency Injection (DI) to manage components and these objects are called Spring Beans. The container uses configuration metadata which represent by Java code, annotations or XML along with Java POJO classes as seen below.
Do you know Features of Spring Framework?
3. Types of IoC Containers in Spring
This are the two types of Spring IoC Containers, let’s see one by one:
a. Spring BeanFactory Container
Spring BeanFactory Container is the simplest container which provides basic support for DI. It is defined by org.springframework.beans.factory.BeanFactory interface. There are many implementations of BeanFactory interface that come with Spring where XmlBeanFactory being the most commonly used class. XmlBeanFactory reads configuration metadata from XML file for creating a fully configured application.
The BeanFactory container prefer, where resources are limited to mobile devices or applet-based applications.
Let’s learn Spring Framework Architecture with Modules
You will look at a working example with Eclipse IDE with the following steps for creating Spring application
- Create a project with a name SpringExample and a package packagecom.example. These should be under src folder of the created project.
- Add the needed Spring libraries using Add External JARs.
- Create Java classes HelloWorld and MainApp under the package packagecom .example.
- Create Beans config file Beans.xml under src folder.
- At last, create content of all Java files and Beans configuration file and run the file as below.
The code of HelloWorld.java is as shown.
package com.example; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
The following is the code of MainApp.java.
package com.example;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class MainApp {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("Beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();
}
}Â
There are some points which should be taken about the main program:
- Write a factory object where you have used APIXmlBeanFactory() to load bean config file in CLASSPATH.
- Use getBean() which uses bean ID to return a generic object to get the required bean.
Let’s See What is Spring Boot CLI with Example
Following is the XML code for Beans.xml.
<?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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "helloWorld" class = "com.example.HelloWorld"> <property name = "message" value = "Hello World!"/> </bean> </beans>
After you run the application you will see the following message as output.
Your Message: Hello World!
b. Spring ApplicationContext Container
The ApplicationContext container is Spring’s advanced container. It is defined by org.springframework.context.ApplicationContext interface. The ApplicationContext container has all the functionalities of BeanFactory. It is generally recommended over BeanFactory. The most common implementations of ApplicationContext are:
- FileSystemXmlApplicationContext: It is a type of container which loads the definitions of beans from an XML file. For that, you should be able to provide the full path of the XML bean config file to a constructor.
- ClassPathXmlApplicationContext: This type of container loads definitions of the beans from XML file but you don’t need to provide the full path of the XML file. Only the CLASSPATH has to set properly as this container will look like Bean config XML file.
- WebXmlApplicationContext: This type of container loads the XML file with all bean definitions within a web application.
i. Working of Eclipse IDE
You will better understand with a working example in Eclipse IDE with the following steps:
- Create a project with a name SpringExample and a package packagecom.example. These should under src folder of the created project.
- Add the needed Spring libraries using Add External JARs.
- Create Java classes HelloWorld and MainApp under the package packagecom .example.
- Create Beans config file Beans.xml under src folder.
- At last, create content of all Java files and Beans configuration file and run the file as below.
Follow this link this know more about Spring JDBC Framework – JDBC Template with Eclipse IDE
The code for HelloWorld.java file:
package com.example; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }
The code for MainApp.java:
package com.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext ("C:/Users/ADMIN/workspace/HelloSpring/src/Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); } }
ii. Some Points
Some points should note about the main program:
- Using framework API FileSystemXmlApplicationContext create a factory object. This API takes care of creating and initializing all objects.
- Use getBean() which uses bean ID to return a generic object to get the required bean.
The code for Beans.xml is as given:
<?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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "helloWorld" class = "com.example.HelloWorld"> <property name = "message" value = "Hello World!"/> </bean> </beans>
After creating source and bean config files, run the application. You will see the following as your output.
Your Message: Hello World!
So, this was all about Spring IoC Containers. Hope you like our explanation.
4. Conclusion
Hence, in this Spring Containers tutorial, we learned two types of Spring IoC Containers:Â BeanFactory Container and ApplicationContext Container in Spring Framework. Furthermore, if you have any query regarding IoC Containers, feel free to ask in the comment section.
See Also- Spring MVC Framework
For reference
Did you like this article? If Yes, please give DataFlair 5 Stars on Google
Will be great if you could clarify as to which container should be used under which condition.
By Default which container will create Beanfactory or ApplicationContaxt ?