Java ProgrammingLearn About IOC Containers in Spring Framework

Learn About IOC Containers in Spring Framework

IOC Containers

In this chapter, we are going to discuss about the IOC container present in the Spring Framework. Spring framework is very popular because of the IOC container. This container is responsible for the objects creation, wiring the objects together, configuring these objects and handling the entire life cycle of these objects from their creation until they are completely destroyed. The container has the Dependency Injection (DI) which is used to manage the components present in an application. Such objects are known as Spring Beans.

Inside the container, the source of instructions to the objects for their instantiation, configuration and assembly are provided through configuration metadata. The configuration metadata for the Spring IOC container can be specified through XML, Java Code, or Java Annotations. The process is explained below with the help of a diagram. It is a high level view on how the Spring IOC container operates. The IOC container uses the POJO class as well as the configuration metadata in the form XML or annotation metadata to produce a complete well configured and executable application system.

IOC Container Operation

Types of Containers in spring Framework

There are two types of containers present in the Spring Framework.

1. Spring BeanFactory Container:
It is the simplest container present in spring framework which provides the basic support for DI (Dependency Injection). We use the following interface to work with this container. org.springframework.beans.factory.BeanFactory
The BeanFactory interface works with other related interfaces such as BeanFactoryAware, InitializingBean, and DisposableBean. These interfaces provide backward compatibility with various third-party frameworks when they are integrated with spring framework.

2. Spring ApplicationContext Container:
This is another container present in the spring container which adds extra enterprise-specific functionality. These functionalities include the capability to resolve textual messages from a properties file and publishing application events to the attentive event listeners. We use the following interface to work with this container. org.springframework.context.ApplicationContext.

BeanFactory vs ApplicationContext Container
The ApplicationContext container is the superset of BeanFactory container. It means that all functionality of the BeanFactory container are present in the ApplicationContext container. Therefore, ApplicationContext container is often preferred over BeanFactory container. BeanFactory are often used to build lightweight applications such as mobile devices, applet-based applications, etc. where data volume as well as speed is important. Below are the most commonly used ApplicationContext implementations.

• FileSystemXmlApplicationContext – Here, we have to provide the full path of the XML bean configuration file to the constructor in order to load the metadata of the beans from an XML file.
• ClassPathXmlApplicationContext – Here, we need to set CLASSPATH of the bean configuration XML file in order to load the metadata of the beans from an XML file.
• WebXmlApplicationContext – Here, the container loads the XML file within a web application which has metadata of all beans.

Example on Spring BeanFactory Container
Creating a spring BeanFactory container based application is very simple, we have to obey the following steps.
1. Create a Maven project using eclipse as we have done in the earlier chapter of this tutorial.
2. Next, add the required spring framework dependencies in the pom.xml file
3. Next, create a Java class “HelloBeanFactory.java” under the “src/main/java” directory of Maven Project which has a property as “msg” and associated setter and getter methods to this property as shown below.

package com.eduonix.springframework.ioc;
/**
 * 
 * @author Aparajita
 *
 */
public class HelloBeanFactory {

	   private String msg;  
	   
	   public void setMsg(String msg){ 
	      this.msg  = msg; 
	   }  
	   public void getMsg (){ 
	      System.out.println("Response Message : " + msg); 
	   } 
}

4. Create a spring BeanFactory container configuration file “bean-factory-config.xml” under “src/main/resources” directory of Maven Project where we have defined the configuration metadata for BeanFactory container as shown below.

<?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 = "helloBeanFactory" class = "com.eduonix.springframework.ioc.HelloBeanFactory">
      <property name = "msg" value = "Welcome to BeanFactory Container Demonstration!"/>
   </bean>

</beans>

5. Lastly, create a Java class “App.java” under “src/main/java” directory of Maven Project where we are going to load the Bean configuration through XmlBeanFactory class and through Bean container we are going to instantiate the Bean object “HelloBeanFactory” and call the ‘getMsg’ method which will display the value configured in the Bean configuration file “bean-factory-config.xml”.

package com.eduonix.springframework.ioc;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource; 

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {
		XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("bean-factory-config.xml"));
		HelloBeanFactory obj = (HelloBeanFactory) factory.getBean("helloBeanFactory");
		obj.getMsg();
	}
}

Output: –
When we will execute the App.java class as a Java application in eclipse, then we will observe the following output.

Dec 02, 2017 7:56:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [bean-factory-config.xml]
Response Message: Welcome to BeanFactory Container Demonstration!

Example on Spring ApplicationContext Container
Creating a spring ApplicationContext container based application requires the following steps to be obeyed.
1. Create a Maven project using eclipse as we have done in the earlier chapter of this tutorial.
2. Next, add the required spring framework dependencies in pom.xml file
3. Next, create a Java class “HelloApplicationContext.java” under “src/main/java” directory of Maven Project which has a property as “msg” and associated setter and getter methods to this property as shown below.

package com.eduonix.springframework.applicationcontext;
/**
 * 
 * @author Aparajita
 *
 */
public class HelloApplicationContext {

	   private String msg;  
	   
	   public void setMsg(String msg){ 
	      this.msg  = msg; 
	   }  
	   public void getMsg (){ 
	      System.out.println("Response Message : " + msg); 
	   } 
}

4. Create a spring ApplicationContext container configuration file “bean-factory-config.xml” under the “src/main/resources” directory of Maven Project where we have defined the configuration metadata for ApplicationContext container as shown below.
The bean-factory-config.xml file

<?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 = "helloApplicationContext" class = "com.eduonix.springframework.applicationcontext.HelloApplicationContext">
      <property name = "msg" value = "Welcome to FileSystemXmlApplicationContext Demonstration!"/>
   </bean>

</beans>

5. Lastly, create a Java class “App.java” under “src/main/java” directory of Maven Project where we are going to load the ApplicationContext configuration through the FileSystemXmlApplicationContext class (here configuration file path is provided) and through ApplicationContext container we are going to instantiate the Bean object “HelloApplicationContext” and call the ‘getMsg’ method which will display the value configured in the Bean configuration file “bean-factory-config.xml”.

package com.eduonix.springframework.applicationcontext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * 
 * @author Aparajita
 *
 */
public class App {
	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext(
				"C:/work/IOC Containers/springframework.applicationcontext/src/main/resources/bean-factory-config.xml");

		HelloApplicationContext obj = (HelloApplicationContext) context.getBean("helloApplicationContext");
		obj.getMsg();
	}
}

Output: –
When we execute the App.java class as a Java application in eclipse, then we will observe the following output.

Dec 02, 2017 8:17:44 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.[email protected]377dca04: startup date [Sat Dec 02 20:17:44 EST 2017]; root of context hierarchy
Dec 02, 2017 8:17:44 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\work\IOC Containers\springframework.applicationcontext\src\main\resources\bean-factory-config.xml]
Response Message: Welcome to FileSystemXmlApplicationContext Demonstration!

Source code for this IOC spring framework blog

Conclusion: –
In this tutorial, we discussed the IOC container present in spring framework and learnt about both types of containers i.e. BeanFactory and ApplicationContext containers with the help of suitable examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,210FollowersFollow
104,000SubscribersSubscribe

More article

- Advertisement -