SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PPT
AspMVC4 start101
Rich Helton
 
DOCX
Struts notes
Rajeev Uppala
 
PDF
Leverage Hibernate and Spring Features Together
Edureka!
 
PDF
Exploring Maven SVN GIT
People Strategists
 
PDF
Working with Servlets
People Strategists
 
PDF
Spring Framework -I
People Strategists
 
PPTX
Spring MVC framework
Mohit Gupta
 
PPT
Tumbleweed intro
Rich Helton
 
PPTX
Introduction to Spring Boot
Purbarun Chakrabarti
 
PDF
The Basic Concept Of IOC
Carl Lu
 
PPTX
Spring & hibernate
Santosh Kumar Kar
 
PPTX
Mike Taulty OData (NxtGen User Group UK)
ukdpe
 
PDF
.NET Core, ASP.NET Core Course, Session 14
Amin Mesbahi
 
PDF
.NET Core, ASP.NET Core Course, Session 18
Amin Mesbahi
 
PPSX
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
PDF
Spring aop
Hamid Ghorbani
 
PDF
Hibernate Interview Questions
Syed Shahul
 
PDF
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Raghavan Mohan
 
KEY
IoC with PHP
Chris Weldon
 
AspMVC4 start101
Rich Helton
 
Struts notes
Rajeev Uppala
 
Leverage Hibernate and Spring Features Together
Edureka!
 
Exploring Maven SVN GIT
People Strategists
 
Working with Servlets
People Strategists
 
Spring Framework -I
People Strategists
 
Spring MVC framework
Mohit Gupta
 
Tumbleweed intro
Rich Helton
 
Introduction to Spring Boot
Purbarun Chakrabarti
 
The Basic Concept Of IOC
Carl Lu
 
Spring & hibernate
Santosh Kumar Kar
 
Mike Taulty OData (NxtGen User Group UK)
ukdpe
 
.NET Core, ASP.NET Core Course, Session 14
Amin Mesbahi
 
.NET Core, ASP.NET Core Course, Session 18
Amin Mesbahi
 
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Spring aop
Hamid Ghorbani
 
Hibernate Interview Questions
Syed Shahul
 
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Raghavan Mohan
 
IoC with PHP
Chris Weldon
 

Viewers also liked (11)

DOCX
Junit With Eclipse
Sunil kumar Mohanty
 
ODP
Dependency Injection in Spring in 10min
Corneil du Plessis
 
PDF
Spring and dependency injection
Steve Ng
 
PPTX
Spring dependency injection
srmelody
 
PPTX
Types of containers
Sagar Gadhiya(PaTel)
 
PPTX
Introduction to Spring Framework
Serhat Can
 
PPTX
Types of containers
MAX GALARZA HERNANDEZ
 
PPT
A Brief presentation on Containerisation
subhash_ae
 
PDF
Spring Framework - Core
Dzmitry Naskou
 
PPTX
Secure Your REST API (The Right Way)
Stormpath
 
PPT
Java EE and Spring Side-by-Side
Reza Rahman
 
Junit With Eclipse
Sunil kumar Mohanty
 
Dependency Injection in Spring in 10min
Corneil du Plessis
 
Spring and dependency injection
Steve Ng
 
Spring dependency injection
srmelody
 
Types of containers
Sagar Gadhiya(PaTel)
 
Introduction to Spring Framework
Serhat Can
 
Types of containers
MAX GALARZA HERNANDEZ
 
A Brief presentation on Containerisation
subhash_ae
 
Spring Framework - Core
Dzmitry Naskou
 
Secure Your REST API (The Right Way)
Stormpath
 
Java EE and Spring Side-by-Side
Reza Rahman
 
Ad

Similar to Spring IOC advantages and developing spring application sample (20)

PPT
Spring
s4al_com
 
PPT
Spring talk111204
s4al_com
 
PDF
An introduction to the Spring Framework
weili_at_slideshare
 
PDF
Introduction to Spring Framework
Rajind Ruparathna
 
PPT
Spring talk111204
ealio
 
PPT
Hybernat and structs, spring classes in mumbai
Vibrant Technologies & Computers
 
PPTX
Spring framework IOC and Dependency Injection
Anuj Singh Rajput
 
PPT
Spring introduction
AnilKumar Etagowni
 
PPT
SpringIntroductionpresentationoverintroduction.ppt
imjdabhinawpandey
 
PPTX
Spring framework
Rajkumar Singh
 
PDF
2-0. Spring ecosytem.pdf
DeoDuaNaoHet
 
PPTX
Spring framework
Sonal Poddar
 
PPTX
Brownbag001 spring ioc from 2012
Timothy Spann
 
PPT
Spring framework
Ajit Koti
 
PPTX
Skillwise-Spring framework 1
Skillwise Group
 
PPTX
Spring (1)
Aneega
 
PDF
Overview chap1
Guo Albert
 
ODP
SPRING 1. overview
Francesco Ierna
 
PPT
Spring frame work
husnara mohammad
 
Spring
s4al_com
 
Spring talk111204
s4al_com
 
An introduction to the Spring Framework
weili_at_slideshare
 
Introduction to Spring Framework
Rajind Ruparathna
 
Spring talk111204
ealio
 
Hybernat and structs, spring classes in mumbai
Vibrant Technologies & Computers
 
Spring framework IOC and Dependency Injection
Anuj Singh Rajput
 
Spring introduction
AnilKumar Etagowni
 
SpringIntroductionpresentationoverintroduction.ppt
imjdabhinawpandey
 
Spring framework
Rajkumar Singh
 
2-0. Spring ecosytem.pdf
DeoDuaNaoHet
 
Spring framework
Sonal Poddar
 
Brownbag001 spring ioc from 2012
Timothy Spann
 
Spring framework
Ajit Koti
 
Skillwise-Spring framework 1
Skillwise Group
 
Spring (1)
Aneega
 
Overview chap1
Guo Albert
 
SPRING 1. overview
Francesco Ierna
 
Spring frame work
husnara mohammad
 
Ad

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 

Spring IOC advantages and developing spring application sample

  • 1. 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) {
  • 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 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.
  • 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(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;
  • 6. 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
  • 7. 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