Site icon DataFlair

Spring IoC Containers – Types of Spring Container

We offer you a brighter future with FREE online courses - 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.

Spring IoC Containers – Types of Spring Container

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?

Spring IoC Containers – Types of Spring Container

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

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);
}
}

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:

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:

i. Working of Eclipse IDE

You will better understand with a working example in Eclipse IDE with the following steps:

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:

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

Exit mobile version