Reading:  

Quick to the point introduction to Spring Framework


IOC Containers

IoC (Inversion of Control) Container:

The container is core of framework of Spring which is responsible to create the objects, configure, and also manage their lifecycle from start to till end of it. From the XML file the IoC container gets the data/information and works accordingly. The main tasks performed by IoC container are:

  • the application class instantiation
  • the object configuration

The high-level workflow of spring is as shown in below diagram; The IoC container makes use classes of Java POJO and metadata configuration to produce a completely configured and executable application.

 

Types of IoC containers:

  • BeanFactory
  • ApplicationContext

Using BeanFactory:

The BeanFactory is a container which provides support for DI and defined by the interface org.springframework.beans.factory.BeanFactory. User need to create the instance of XmlBeanFactory class to use the BeanFactory. This container gets metadata configuration from an XML file and uses it to create a completely configured application.

Example:

To create the java class, Right click on 'src', click 'New' choose 'class'; specify the class name e.g. Customer and click 'finish'.

package pack1;  
  
public class Customer {  
private String cust_name;  
  
public String getCname() {  
    return cust_name;  
}  
  
public void setCname(String cust_name) {  
    this.cust_name = cust_name;  
}  
  
public void displayMessage(){  
    System.out.println("Account holder name: "+cust_name);  
    }  
}  
 

To create the configuration xml file right click on project 'src' select 'new' choose ‘file’ - give the file name as Beans.xml click 'finish'.

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
<bean id="custbean" class="pack1.Customer">  
<property name=" cust_name " value="Nimrit"></property>  
</bean>  
  
</beans>  

 

Create the java class ExmTest. Here the two important points to note:

  1. The framework API XmlBeanFactory() is used to create the factory bean and ClassPathResource() API to load the bean configuration file available in CLASSPATH. The XmlBeanFactory() API takes care of creating and initializing all the objects.
  2. By using getBean() method of the created bean factory object. This method uses bean ID to return a generic object which can be casted to actual object.
package pack1;  
  
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
  
public class ExmTest {  
public static void main(String[] args) {  
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));
   
    Customer customer=(Customer)factory.getBean("custbean");  
    customer.displayMessage();  
     }  
}  
 

 

Compile and run the program will get the below result:

Result:

Account holder name: Nimrit

 

Using ApplicationContext:        

 

The ApplicationContext is similar to the BeanFactory it can load definitions of bean, wire beans together and upon request dispense beans. The class ClassPathXmlApplicationContext is the implementation of interface ApplicationContext. User needs to instantiate the class ClassPathXmlApplicationContext to use the ApplicationContext.

The most commonly used ApplicationContext implementations are:

  • ClassPathXmlApplicationContext This container loads the definitions of the beans from an XML file. Here user need not to provide the full path of the XML file but need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
  • FileSystemXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here user needs to provide the full path of the XML bean configuration file to the constructor.
  • WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.

Example:

To create the java class, Right click on project 'src', click 'New' choose 'class'; specify the class name e.g. Customer and click 'finish'.

package pack1;  
  
public class Customer {  
private String cust_name;  
  
public String getCname() {  
    return cust_name;  
}  
  
public void setCname(String cust_name) {  
    this.cust_name = cust_name;  
}  
  
public void displayMessage(){  
    System.out.println("Account holder name: "+cust_name);  
    }  
}  
 

To create the xml file click on ‘src’ select ‘new’ choose ‘file’ - give the file name as Beans.xml click ‘finish’.

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
<bean id="custbean" class="pack1.Customer">  
<property name=" cust_name " value="Suman"></property>  
</bean>  
  
</beans>  

 

Create the java class ExmTest. Here the two important points to note:

  1. User uses the framework API FileSystemXmlApplicationContext to create the factory bean after loading the bean configuration file from the given path. The FileSystemXmlApplicationContext() API takes care of creating and initializing all the objects.
  2. By using getBean() method of the created bean factory object. This method uses bean ID to return a generic object which can be casted to actual object.
package pack1;  
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
  
public class ExmTest {  
public static void main(String[] args) {  
ApplicationContext context = new FileSystemXmlApplicationContext
            ("C:/Users/workspace/JavaExm/src/Beans.xml");

   
    Customer customer=(Customer)factory.getBean("custbean");  
    customer.displayMessage();  
     }  
}  
 

Compile and run the program will get the below result:

Result:

Account holder name: Suman

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!