Spring IOC Containers
In this tutorial, we are going to discuss the Spring IOC containers. The main intention of Spring IOC Containers is to read bean configurations from a configuration file, create Bean objects and Provide bean Objects to Spring applications.
There are two types of IOC Containers in the Spring framework.
- BeanFactory
- ApplicationContext
1. BeanFactory
- It is the fundamental or Base IOC container provided by Spring Framework to manage bean objects.
- BeanFactory IOC container will provide basic functionalities to the spring framework by creating maintaining beans objects as per the beans configuration details in the spring beans configuration file.
- To represent BeanFactory IOC Container, Spring framework has provided an interface in the form of “org.springframework. beans.factory.BeanFactory”.
- For the BeanFactory interface, Spring Framework has provided an implementation class in the form of org.springframework. beans.factory.xml.XmlBeanFactory.
- If we want to use BeanFactory IOC Container in Spring applications, we have to use the following steps.
- Create Resource Object
- Create BeanFactory object
- Get Bean and access Business Method.
1. Create Resource Object
- A resource is an object in Spring Framework. It can represent all bean configuration details which we provided in beans configurations details.
- To represent Resource object, Spring Framework has provided a predefined interface in the form of “org.springframework. core.io.Resource”
- For the Resource interface, Spring Framework has provided the following implementation classes.
org.springframework.core.io.ByteArrayResource: It can represent all the beans configuration details available in the form of byte [].
org.springframework.core.io.FileSystemResource: It can get all the beans configuration details available in the form of a file in our system hard disk.
org.springframework.core.io.ClassPathResource: It can get all the beans configuration details that exist at the “classpath” environment variable referred locations.
org.springframework.core.io.InputStreamResource: It can get all the beans configurations which are existed in the form of InputStream.
org.springframework.core.io.UrlResource: It can get all the beans configuration details that exist at a particular URL in the network.
org.springframework.web.context.support.ServletContextResource: It can get all the beans configuration details which are existed in ServletContext. It will be used in spring web applications.
org.apringframework.web.portlet.context.PortletContextResource: It can get all the beans configuration details which are existed in PortletContext. It will be used in spring web applications designed based on portlets.
E.g
Resource res = new ClassPathResource("beans.xml");
2. Create BeanFactory Object
To create an XmlBeanFactory class object, we have to use the following constructor.
public XmlBeanFactory(Resource res)
E.g
BeanFactory factory = new XmlBeanFactrory(res);
3. Get Bean object from BeanFactory and access business method
To get Bean object from BeanFactory we have to use the following method.
public Object getBean(String id_Name)
E.g
Employee emp = (Employee) factory.getBean("employee");
Note
BeanFactory is deprecated in Spring3.x version.
E.g
Bean Class
package com.ashok.spring.core.ioc.beanfactory.beans;
/**
*
* @author Ashok Kumar
*
*/
public class Employee {
private String empName;
private String empId;
private String empAddress;
private double salary;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
+ "]";
}
}
Config 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 = "emp" class = "com.ashok.spring.core.ioc.beanfactory.beans.Employee">
<property name = "empName" value = "Ashok Kumar"/>
<property name = "empId" value = "Emp0087"/>
<property name = "empAddress" value = "Bhimavaram"/>
<property name = "salary" value = "47000"/>
</bean>
</beans>
Client Application
package com.ashok.spring.core.ioc.beanfactory.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.ashok.spring.core.ioc.beanfactory.beans.Employee;
/**
*
* @author Ashok Kumar
*
*/
@SuppressWarnings("deprecation")
public class TestSpringApplication {
public static void main(String[] args) {
String configFile = "/com/ashok/spring/core/ioc/beanfactory/config/beans.xml";
Resource resource = new ClassPathResource(configFile);
BeanFactory factory = new XmlBeanFactory(resource);
Employee emp = (Employee) factory.getBean("emp");
System.out.println(emp);
}
}
In the above application, when we activate the BeanFactory container, then BeanFactory Container will be started. It will not create any Bean object immediately, and it will perform the following actions.
- It will take the bean configuration file name and location from the Resource object.
- It will search for the respective bean configuration file at the specified location.
- If the respective bean configuration file is available, then the BeanFactory container will load that XML file to the memory.
- After XML file loading, BeanFactory Container will parse that XML file, i.e., it will check all the tags in the XML file are provided properly or not, all attributes are available properly or not.
- After the XML file parsing, BeanFactory Container will read data from the Beans configuration file and store it in the Resource object.
After getting BeanFactory Object, when we access the getBean(-) method, then BeanFactory will perform the following actions.
- BeanFactory will search for the respective Bean configuration in the Resource object based on the provided identity.
- If any bean configuration is identified in the Resource object based on the provided identity, the BeanFactory container will take the respective bean class name and location.
- BeanFactory Container will search for the respective bean class at the specified location. If it is available, then BeanFactory Container will load all the bean class bytecode to the memory.
- BeanFactory Container will create an Object for the loaded bean class and its dependent bean objects.
- BeanFactory Container will store the generated bean object, and its dependent objects in the Container object in the form of Key-Value pairs, where keys must be the “id” attribute values specified in the beans configuration file and values are Bean Objects.
2. ApplicationContext
ApplicationContext IOC Container is an extension of BeanFactory IOC Container. It can provide some advanced features like Internationalization, Event Handling etc., along with fundamental functionalities that BeanFactory provides. In Spring, ApplicationContext IOC Container is represented in the form of the following predefined interface.
"org.springframework.context.ApplicationContext".
Spring Framework has provided ApplicationContext as a child interface to the BeanFactory interface. Spring Framework has provided the following three implementation classes for ApplicationContext.
1. ClassPathXmlApplicationContext
It can get all the beans configuration details from the configuration file in the application classpath.
2. FileSystemXmlApplicationContext
It can get all the beans configuration details from the Configuration file, which is existed on our system hard disk.
3. WebXmlApplicationContext
It can get all the beans configuration details from the configuration file in the web application.
E.g
Bean Class
package com.ashok.spring.core.ioc.applicationcontext.beans;
/**
*
* @author Ashok Kumar
*
*/
public class Employee {
private String empName;
private String empId;
private String empAddress;
private double salary;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
+ "]";
}
}
Config File (applicationContext.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 = "emp" class = "com.ashok.spring.core.ioc.applicationcontext.beans.Employee">
<property name = "empName" value = "Ashok Kumar"/>
<property name = "empId" value = "Emp0087"/>
<property name = "empAddress" value = "Bhimavaram"/>
<property name = "salary" value = "50000"/>
</bean>
</beans>
Client Application
package com.ashok.spring.core.ioc.applicationcontext.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ashok.spring.core.ioc.applicationcontext.beans.Employee;
/**
*
* @author Ashok Kumar
*
*/
public class TestSpringApplication {
@SuppressWarnings("resource")
public static void main(String[] args) {
String configFile = "/com/ashok/spring/core/ioc/applicationcontext/config/applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
Employee emp = (Employee) context.getBean("emp");
System.out.println(emp);
}
}
When we activate the ApplicationContext container in the above application, then ApplicationContext Container will perform the following actions.
- It will take the bean configuration file name and location from the Container class constructor.
- It will search for the respective bean configuration file at the specified location.
- If the respective bean configuration file is available, the ApplicationContext container will load that XML file to the memory.
- After XML file loading, ApplicationContext Container will parse that XML file, i.e., it will check all the tags in the XML file are provided properly or not, all attributes are available properly or not.
- After the XML file parsing, ApplicationContext Container will read data from the Beans configuration file.
- If any bean configuration is identified in the beans configuration file, the ApplicationContext container will take beans classes and their locations.
- ApplicationContext Container will search for the respective beans at the specified locations. If they are available, then IOC Container will load all the bean classes bytecode to the memory.
- ApplicationContext Container will create Objects for the loaded bean classes and their dependent bean objects.
- ApplicationContext Container will store all the bean objects and their dependent objects in the Container object in the form of Key-Value pairs, where keys must be the “id” attribute values specified in the beans configuration file.
In the above context, if we access the getBean(“–“) method over Container reference, then ApplicationContext Container will search for the Bean object based on the provided bean identity. If it is available, then AppliationContext Container will return the Bean object.
Differences between BeanFactory and ApplicationContext IOC Containers
- BeanFactory is a fundamental IOC Container. It can provide fundamental functionalities to the spring applications like creating and maintaining bean objects.
ApplicationContext IOC Container is an extension of BeanFactory IOC Container. It can provide some advanced features like Internationalization, Event Handling etc., along with fundamental functionalities that BeanFactory provides. - BeanFactory is not supporting integrating AOP services like Security, JTA etc., to the spring applications.
ApplicationContext is supporting to integrating AOP services like Security, JTA etc., into the spring applications. - BeanFactory is not suitable for web applications that we will prepare based on the Spring web module.
ApplicationContext is suitable for the web applications which we want to prepare based on the Spring web module. - BeanFactory can prepare Singleton objects when we send the first request for bean, Lazy Instantiation/Initialization.
ApplicationContext can prepare Singleton objects when we activate Container, that is, early Instantiation/Initialization. - BeanFactory supports only the scopes like Singleton and Prototype.
ApplicationContext supports almost all the Spring scopes like Singleton, Prototype, request, session, global session, WebSocket etc. - BeanFactory is mainly for Standalone Applications.
ApplicationContext is for all types of Spring framework applications. - BeanFactory is an outdated Container in Spring applications.
ApplicationContext is not an outdated Container.