Advantage of Spring IOC over traditional approach & Developing 
spring application sample 
The Core concept of spring is IOC[ Inversion of control ] . IoC or Inversion of Control, is also 
known as Dependency Injection. It is used to create loosely coupled components. The below 
example best describes the traditional approach vs loose coupling model . 
Example : 
Suppose there exists a class known as ImageFileWriter that creates a file and writes the Image 
bytes to the File . The ImageFileWriter gets the image data from from another class known as 
ImageWriter and in response it returns the instance of the file to which it writes the data. 
For this situation the ImageWriter need to call the 'writeToFile' method on the instance of 
ImageFileWriter class. The scenario in this case will be like the following : 
The code in the ImageWriter class for the above scenario will be :- 
I ImageFilewriter imagefileWriter = new ImageFileWriter(); 
imageFileWriter.writeToFile(byteBuffer); 
Here if we are going to take a close look , then we can find that there remains a tight 
dependency between the ImageWriter and ImageFileWriter class. ImageWriter class is 
dependent on the ImageFileWriter class. If in future for some reason the “ImageFileWriter” 
class will be changed to ImagePdfFileWriter” , then we need to change the source code of 
the “ImageWriter” class as well. 
Advantage of IOC here : 
IoC solves the above problem of tight couple and helps to makes the software component 
loosely coupled, so that we can maintain our code pretty much easily . We can modify the code 
any instance of time without hampering the other classes which referring to it . 
Inversion of Control is a design pattern that provides instances of a bean or class to 
another when they needed. No need to use new keyword to create instance of dependent class. 
Only just create a reference of dependent class and IoC pattern will set the instance to the 
reference when needed . 
Now using IOC concept the code inside ImageWriter class will be : 
ImageFileWriter imageFileWriter = null; 
public void setImageFileWriter(ImageFileWriter imageFileWriter) {
this.imgaeFileWriter = imagFileWriter; 
} 
IoC container will call setter method to set the instance of ImageFileWriter class. We have to 
configure the dependency IoC configuration file. IOC configuration file indicates the IoC 
container when and how to inject the dependences. 
Creating a completely independent model : 
Here we will find how to use IoC to create effective independent components. Considering the 
last ImageWriter class. There exists an interface named “GenericBufferedWriter” which 
contains following definition : 
public interface GenericBufferedWriter { 
public File writeToFile(byte buffer); 
} 
Any class implementing the above interface need to have the “writeToFile” as per it'd own way. 
Now the code of the ImageWriter class will be as follows. 
private GenericBufferedWriter genericBufferedwriter = null; 
public void setGenericBuffer(GenericBufferedwriter 
ImageWriter) { 
this.genericBufferWriter = ImageWriter; 
} 
// Some codes 
genericBufferedWriter.writeToFile(bytes); 
Suppose we want to types of output . For the 1st scenario the image bytes need to be written into 
a JPEG file and in the second situation the image need to be generated as a PNG file. In this 
situation we can create two classes which JPEGBufferedWriter as well as PNGBufferedWriter 
and both the classes need to implement the GenericBufferedwriter interface. Here we can 
configure the IOC container in such a manner that ioc container will provide the instance of J 
PEGBufferedWriter and PNGBufferedWriter as per the requirement. This one is an generalized 
structure and this is an advantage of spring IOC design pattern. 
Spring container overview : 
Spring container is the core of Spring Framework. The core container manages the The 
container objects till the end of there life cycle. It is responsible for creating, wiring them 
together and configuring them, The Spring container uses dependency injection (DI) to 
manage the components that make up an application. The container gets its instructions on what 
objects to instantiate, configure, and assemble by reading configuration metadata provided. The 
configuration metadata can be represented either by XML, Java annotations, or Java code. We 
can see over here that it does not forces the developer to use any particular type of
configuration file like most of the frame work supports xml file configuration type. It also helps 
to avoid the singleton programming. 
The following diagram shows the spring Container architecture : 
Initializing spring Core container and accessing Spring Beans: 
The Spring core container can be instantiated by creating an object of BeanFactory or 
ApplicationContext implementation classes supplying the spring bean configurations. The 
different types of BeanFactory are classified based on the spring bean configuration format they 
used to understand and the way they locate the configurations. Here is simple code snippet 
describes how to instantiate the the spring container. 
Using BeanFactory to instantiate spring core container : 
BeanFactory beans = new XmlBeanFactory(new FileSystemResources 
("demobeans.xml")); 
In the above code snippet we are using the XMLBeanFactory implementation for the 
instantiating the the spring container with “demobeans.xml” file aa a configuration file. We can 
even use application context to instantiate the container. 
Using ApplicationContext to instantiate spring core container : 
ApplicationContext context = new 
ClassPathXmlApplicationContext("demobeans.xml"); 
In this case classPathApplicationContext is used to instantiate the spring container with 
“demobeans.xml” . The ClassPathApplicationContext locates the the XML configuration file in 
the classpath . Once after initializing the container we can use BeanFactory object method to 
access the spring beans.
Developing sample application :- 
From the above discussion now we are having overview on Basic IOC concept along with the 
spring container . Based on the above basic concept below a simple example is present 
demonstrating the basic XML configuration and instantiating the spring core container. This 
example contains the following parts . 
DemoService.java : 
This is a simple plain java class which can be used anywhere without any modification 
demobeans.xml : 
This is the XML configuration file for this example. 
DemoServiceTest.java : 
This class having the main method that instantiates the spring core container and access 
the bean defined in the spring bean XML configuration file. 
Note : download the following jar files and use them in the classpath of the application 
spring-beans-3.0.0.RELEASE.jar , spring-context-3.0.2.RELEASE.jar, 
spring-core-3.0.0.RELEASE.jar 
DemoService.java 
package com.spring.demo; 
/** 
* Service class. This is simply a POJO class 
* 
* @author sunilm 
* 
*/ 
public class DemoService { 
String message ; 
/** 
* No argument constructor 
*/ 
public DemoService( ) { 
message = "This is the default message" ; 
} 
/** 
* Parameterized constructor 
* @param msg message
*/ 
public DemoService(String msg) { 
message = msg ; 
} 
/** 
* Get the input message 
* 
* @return the message 
*/ 
public String getMessage( ) { 
return message ; 
} 
} 
demobeans.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 = "demoService1" class = "com.spring.demo.DemoService" /> 
<bean id = "demoService2" class = "com.spring.demo.DemoService"> 
<constructor-arg> 
<value> 
demonstration of xml configuration file 
</value> 
</constructor-arg> 
</bean> 
</beans> 
Description : 
The <beans> tag is the root element of the spring beans xml configuration document. The bean 
tag defines the spring bean that is the Plain Old Java Object to initialize and manage by the 
spring ore container. The xml document of the above example includes two bean definition. Out 
of which one initializes the no argument constructor , which is referred with the name 
'demoService1' and the other initializes the DemoService class using one argument constructor 
passing the message 'demonstration of xml configuration file' as a value where the definition is 
referred to with the name demoService2. 
DemoServiceTest.java 
package com.spring.demo;
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.FileSystemResource; 
/** 
* Demonstration of instantiating the bean factory and accessing the 
spring beans 
* 
* @author sunilm 
* 
*/ 
public class DemoServiceTest { 
public static void main (String args[]) { 
BeanFactory beans = new XmlBeanFactory(new 
FileSystemResource("demobeans.xml")); 
// get the instance of no argument constructor[demoservice1] 
DemoService demoService1 = (DemoService)beans.getBean 
("demoService1"); 
System.out.println("Mesage from the demoService1 bean :"); 
System.out.println(demoService1.getMessage()); 
// get the instance of parameterized constructor[demoservice2] 
DemoService demoService2 = (DemoService)beans 
.getBean("demoService2"); 
System.out.println("Mesage from the demoService2 bean :"); 
System.out.println(demoService2.getMessage()); 
} 
} 
The output of the above application will be as follows :- 
Mesage from the demoService1 bean : 
This is the default message 
Mesage from the demoService2 bean : 
demonstration of xml configuration file
I hope the above session will provide an overall idea on the spring IOC and it's 
advantages . In the next session I will go more details on the various aspects of spring IOC. 
Thanks

Spring IOC advantages and developing spring application sample

  • 1.
    Advantage of SpringIOC over traditional approach & Developing spring application sample The Core concept of spring is IOC[ Inversion of control ] . IoC or Inversion of Control, is also known as Dependency Injection. It is used to create loosely coupled components. The below example best describes the traditional approach vs loose coupling model . Example : Suppose there exists a class known as ImageFileWriter that creates a file and writes the Image bytes to the File . The ImageFileWriter gets the image data from from another class known as ImageWriter and in response it returns the instance of the file to which it writes the data. For this situation the ImageWriter need to call the 'writeToFile' method on the instance of ImageFileWriter class. The scenario in this case will be like the following : The code in the ImageWriter class for the above scenario will be :- I ImageFilewriter imagefileWriter = new ImageFileWriter(); imageFileWriter.writeToFile(byteBuffer); Here if we are going to take a close look , then we can find that there remains a tight dependency between the ImageWriter and ImageFileWriter class. ImageWriter class is dependent on the ImageFileWriter class. If in future for some reason the “ImageFileWriter” class will be changed to ImagePdfFileWriter” , then we need to change the source code of the “ImageWriter” class as well. Advantage of IOC here : IoC solves the above problem of tight couple and helps to makes the software component loosely coupled, so that we can maintain our code pretty much easily . We can modify the code any instance of time without hampering the other classes which referring to it . Inversion of Control is a design pattern that provides instances of a bean or class to another when they needed. No need to use new keyword to create instance of dependent class. Only just create a reference of dependent class and IoC pattern will set the instance to the reference when needed . Now using IOC concept the code inside ImageWriter class will be : ImageFileWriter imageFileWriter = null; public void setImageFileWriter(ImageFileWriter imageFileWriter) {
  • 2.
    this.imgaeFileWriter = imagFileWriter; } IoC container will call setter method to set the instance of ImageFileWriter class. We have to configure the dependency IoC configuration file. IOC configuration file indicates the IoC container when and how to inject the dependences. Creating a completely independent model : Here we will find how to use IoC to create effective independent components. Considering the last ImageWriter class. There exists an interface named “GenericBufferedWriter” which contains following definition : public interface GenericBufferedWriter { public File writeToFile(byte buffer); } Any class implementing the above interface need to have the “writeToFile” as per it'd own way. Now the code of the ImageWriter class will be as follows. private GenericBufferedWriter genericBufferedwriter = null; public void setGenericBuffer(GenericBufferedwriter ImageWriter) { this.genericBufferWriter = ImageWriter; } // Some codes genericBufferedWriter.writeToFile(bytes); Suppose we want to types of output . For the 1st scenario the image bytes need to be written into a JPEG file and in the second situation the image need to be generated as a PNG file. In this situation we can create two classes which JPEGBufferedWriter as well as PNGBufferedWriter and both the classes need to implement the GenericBufferedwriter interface. Here we can configure the IOC container in such a manner that ioc container will provide the instance of J PEGBufferedWriter and PNGBufferedWriter as per the requirement. This one is an generalized structure and this is an advantage of spring IOC design pattern. Spring container overview : Spring container is the core of Spring Framework. The core container manages the The container objects till the end of there life cycle. It is responsible for creating, wiring them together and configuring them, The Spring container uses dependency injection (DI) to manage the components that make up an application. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code. We can see over here that it does not forces the developer to use any particular type of
  • 3.
    configuration file likemost of the frame work supports xml file configuration type. It also helps to avoid the singleton programming. The following diagram shows the spring Container architecture : Initializing spring Core container and accessing Spring Beans: The Spring core container can be instantiated by creating an object of BeanFactory or ApplicationContext implementation classes supplying the spring bean configurations. The different types of BeanFactory are classified based on the spring bean configuration format they used to understand and the way they locate the configurations. Here is simple code snippet describes how to instantiate the the spring container. Using BeanFactory to instantiate spring core container : BeanFactory beans = new XmlBeanFactory(new FileSystemResources ("demobeans.xml")); In the above code snippet we are using the XMLBeanFactory implementation for the instantiating the the spring container with “demobeans.xml” file aa a configuration file. We can even use application context to instantiate the container. Using ApplicationContext to instantiate spring core container : ApplicationContext context = new ClassPathXmlApplicationContext("demobeans.xml"); In this case classPathApplicationContext is used to instantiate the spring container with “demobeans.xml” . The ClassPathApplicationContext locates the the XML configuration file in the classpath . Once after initializing the container we can use BeanFactory object method to access the spring beans.
  • 4.
    Developing sample application:- From the above discussion now we are having overview on Basic IOC concept along with the spring container . Based on the above basic concept below a simple example is present demonstrating the basic XML configuration and instantiating the spring core container. This example contains the following parts . DemoService.java : This is a simple plain java class which can be used anywhere without any modification demobeans.xml : This is the XML configuration file for this example. DemoServiceTest.java : This class having the main method that instantiates the spring core container and access the bean defined in the spring bean XML configuration file. Note : download the following jar files and use them in the classpath of the application spring-beans-3.0.0.RELEASE.jar , spring-context-3.0.2.RELEASE.jar, spring-core-3.0.0.RELEASE.jar DemoService.java package com.spring.demo; /** * Service class. This is simply a POJO class * * @author sunilm * */ public class DemoService { String message ; /** * No argument constructor */ public DemoService( ) { message = "This is the default message" ; } /** * Parameterized constructor * @param msg message
  • 5.
    */ public DemoService(Stringmsg) { message = msg ; } /** * Get the input message * * @return the message */ public String getMessage( ) { return message ; } } demobeans.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 = "demoService1" class = "com.spring.demo.DemoService" /> <bean id = "demoService2" class = "com.spring.demo.DemoService"> <constructor-arg> <value> demonstration of xml configuration file </value> </constructor-arg> </bean> </beans> Description : The <beans> tag is the root element of the spring beans xml configuration document. The bean tag defines the spring bean that is the Plain Old Java Object to initialize and manage by the spring ore container. The xml document of the above example includes two bean definition. Out of which one initializes the no argument constructor , which is referred with the name 'demoService1' and the other initializes the DemoService class using one argument constructor passing the message 'demonstration of xml configuration file' as a value where the definition is referred to with the name demoService2. DemoServiceTest.java package com.spring.demo;
  • 6.
    import org.springframework.beans.factory.BeanFactory; importorg.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; /** * Demonstration of instantiating the bean factory and accessing the spring beans * * @author sunilm * */ public class DemoServiceTest { public static void main (String args[]) { BeanFactory beans = new XmlBeanFactory(new FileSystemResource("demobeans.xml")); // get the instance of no argument constructor[demoservice1] DemoService demoService1 = (DemoService)beans.getBean ("demoService1"); System.out.println("Mesage from the demoService1 bean :"); System.out.println(demoService1.getMessage()); // get the instance of parameterized constructor[demoservice2] DemoService demoService2 = (DemoService)beans .getBean("demoService2"); System.out.println("Mesage from the demoService2 bean :"); System.out.println(demoService2.getMessage()); } } The output of the above application will be as follows :- Mesage from the demoService1 bean : This is the default message Mesage from the demoService2 bean : demonstration of xml configuration file
  • 7.
    I hope theabove session will provide an overall idea on the spring IOC and it's advantages . In the next session I will go more details on the various aspects of spring IOC. Thanks