What is IOC Container? Spring Framework [Spring Core Module]
IOC CONTAINERS in Spring Framework | How it works | Why IOC Container?:-
The main intention of the IOC container is to read bean configuration from the bean configuration XML file, creating Bean objects, and providing bean Objects to the Spring Application.
There are two types of IOC Containers in Spring Framework:-
1. Bean Factory
2. Application Context
1. BEAN FACTORY :
· It is a fundamental container provided by the Spring framework in order to manage bean objects.
· Bean factory container provides basic functionality to the spring framework by creating, maintaining the beans objects as per the beans configuration details which are provided in the spring beans configuration files.
· To represent bean factory IOC container, spring framework has provided an interface — org.springframework.beans.factory.BeanFactory
· For the BeanFactory interface IOC container, Spring Framework has provided an implementation class in the form of — org.springframework.beans.factory.xml.XMLBeanFactory
· If we want to use BeanFactory in our spring framework then we have to use the following steps :
o I) Create Resource object
o ii) Create BeanFactory object
o iii) Get Bean and access business methods
Resource
A resource is an object in the Spring framework, it is able to represent all bean configuration details which we provide in the beans configuration file.
· To represent Resource spring framework provides an interface — org.springframework.core.io.Resource
· For Resource interface, the spring framework has provided the following implementing classes
1. org.springframework.core.io.ByteArrayResource :
It is able to represent all the configuration details that are available in the form of bytes [].
2. org.springframework.core.io.FileSystemResource :
It is able to get all the beans configuration details that are available in the form of a file in our system.
3. org.springframework.core.io.ClasspathResource :
It is able to get all the beans configuration details which are existed as “classpath” in environment variables.
4. org.springframework.core.io.InputStreamResource :
It is able to get all the configuration details which are existed in the form of InputStream.
5. org.springframework.core.io.UrlResource :
It is able to get all the configuration details which are existed at a particular URL in the network.
6. org.springframework.web.context.support.ServletContextResource :
It is able to get all the configuration details that existed in ServletContext.
It will be used in spring web applications
7. org.springframework.web.portlet.context.PortletContextResource :
It able to get all the beans configuration details which are exited in PortletContext.
It will be used in spring web applications designed on the basis of portlets.
Example:-
Resource res = new ClassPathResource(“beans.xml”);
2) Create BeanFactory Object
To create XMLBeanFactory object we have to use the following constructor :
Public XmlBeanFactory(Resource res)
Ex : BeanFactory factory = new XmlBeanFactory(res);
3) Get Bean object from BeanFactory and use business methods.
To get the Bean object from the BeanFactory we have to use the following method: public Object getBean(String id_name)
Ex : HelloBean bean = (HelloBean)factory.getBean(“helloBean”);
Note : BeanFactory is deprecated in Spring 3.x version.
HelloBean.java
package com.codewithankit.beans;
class HelloBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String sayHello() {
return “Hello” + name;
}
}
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.xsd"><bean name=”helloBean” class=”com.codewithankit.beans.HelloBean”>
</bean>
</beans>
Test.java
package com.codewithankit.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXMLApplicationContext;
import com.codewithankit.beans.HelloBean;
class Test {
public static void main(String[] args) throws Exception {
XmlBeanFactory factory = new
XmlBeanFactory(newClassPathResource(“applicationContext.xml”));
HelloBean bean = (HelloBean)factory.getBean(“helloBean”);
System.out.println(bean.sayHello());
}
}
In the above application, when we activate the BeanFactory container, then BeanFactory container will be started and it will not create any bean object immediately but perform the following actions :
1. It will take the bean configuration file name and location from the Resource object.
2. It will search for the respective bean configuration file at the specified path.
3. If the respective bean configuration file is available then BeanFactory loads the configuration XML file to the memory.
4. After loading, the BeanFactory container will parse that XML file, that is, it will check all the tags in the XML file are provided properly or not, all the attributes are available or not.
5. After parsing, the BeanFactory container will read the data from the bean configuration file and stores it in the Resource object.
After getting the BeanFactory object, when we access the getBean(String id_name) method then BeanFactory will perform :
1. BeanFactory will search for the respective Bean configuration in the Resource object on the basis of the provided identity.
2. If any bean configuration is identified in the Resource object on the basis of the provided identity then the BeanFactory container will take the respective bean class name and its location.
3. BeanFactory container will search for the respective bean class at the specified location, if it is available then the BeanFactory container will load all the bean class bytecode to the memory.
4. BeanFactory container will create objects for the loaded bean class and its dependent bean objects.
5. BeanFactory container will store the generated bean object and its dependent objects in container object in the form of key-value pairs, where keys must be the “id” attribute values which we specified in the bean configuration file and values are Bean objects.
ApplicationContext
ApplicationContext IOC Container is an extension of BeanFactory IOC Container. It is able to provide some advanced features like internationalization, Event Handling……along with fundamental functionalities that BeanFactory is providing.
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 BeanFactory interface.
Spring framework has provided all the 3 implementing classes for ApplicationContext :
1. ClassPathXmlApplicationContext
It is able to get all the beans configuration XML file details which are existed in the classpath.
2. FileSystemXmlApplicationContext
It able to get all the beans configuration XML file details which are existed in our file system hard disk.
3. WebXmlApplicationContext
It is able to get all the beans configuration XML file details which is existed in the web applications.
HelloBean.java
package com.codewithankit.beans;
class HelloBean {
static {
System.out.println(“Bean is loading…..”);
}
public HelloBean() {
System.out.println(“Bean is loading…..”);
}
public String sayHello() {
return “Hello user!!!”;
}
}
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.xsd"><bean id=”helloBean” class=”com.codewithankit.beans.HelloBean”>
</bean>
</beans>
Test.java
package com.codewithankit.test;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.codewithankit.beans.factory.xml.XmlBeanFactory;
class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new
ClasspathXmlApplicationContext(“applicationContext.xml”);
HelloBean bean = (HelloBean)context.getBean(“helloBean”);
System.out.println(bean.sayHello());
}
}
In the above application, when we activate the BeanFactory container then BeanFactory Container will perform the following actions :
1. It will take the bean configuration XML file name and location from the Container class constructor.
2. It will search for the respective bean configuration file at the specified location.
3. If the respective bean configuration file will found, then ApplicationContext will load that XML to the memory.
4. After XML file loading, ApplicationContext will parse that XML file that is, it will check whether all properties are defined well into the XML file.
5. After parsing, now ApplicationContext will read the data from the XML configuration file.
6. If any bean class is identified in the bean configuration XML file, then ApplicationContext will take the bean class and its location.
7. Now, ApplicationContext will find the bean class at the specified location if found, now the IOC container will load all the bean classes bytecode into the memory.
8. ApplicationContext container will create the object for the loaded bean class and its dependent class.
9. ApplicationContext will store the beans object and its dependent class objects in the Container in the form of key-value pairs, where keys must be the value of the ‘id’ attribute which we specified in the bean configuration file.
In the above context, if we access the getBean( — — ) method over the Container reference then ApplicationContext Container will search for the Bean object on the basis of the provided bean identity, it is available then ApplicationContext container will return the bean object.
Q. What are the differences between BeanFactory & ApplicationContext IOC Containers?
1. BeanFactory is a fundamental IOC Container, it is able to provide fundamental functionalities to the spring application like creating & maintaining the bean objects.
ApplicationContext IOC Container is an extension of BeanFactory IOC Container, it is able to provide advanced features like Internationalization, Event Handling….along with fundamental functionalities that BeanFactory is providing.
2. BeanFactory is not supporting integrating AOP services like Security, JTA,…to the spring application.
ApplicationContext is not supporting to integrate AOP services like Security, JTA,… to the spring application.
3. BeanFactory is not suitable for web applications which we are going to prepare on the basis of the Spring web module.
ApplicationContext is suitable for web applications which we are going to prepare on the basis of the Spring web module.
4. BeanFactory is able to prepare Singleton objects when we send request for bean, that is, Lazy Instantiation/Initialization.
ApplicationContext is able to prepare Singleton objects when we activate Container, that is, early Instantiation/Initialization.
5. BeanFactory is only supporting scopes like Singleton & prototype.
ApplicationContext is supporting almost all the Spring scopes like requests, session, globalSession, webSocket etc.
6. BeanFactory is mainly for Standalone applications.
ApplicationContext is for all types of applications in Spring frameworks.
7. BeanFactory is an outdated Container in Spring but ApplicationContext Container is not outdated.