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

AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101
Rich Helton
 
Maven
MavenMaven
Leverage Hibernate and Spring Features Together
Leverage Hibernate and Spring Features TogetherLeverage Hibernate and Spring Features Together
Leverage Hibernate and Spring Features Together
Edureka!
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
Mohit Gupta
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed intro
Rich Helton
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOCCarl Lu
 
Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)
ukdpe
 
.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14
aminmesbahi
 
.NET Core, ASP.NET Core Course, Session 18
 .NET Core, ASP.NET Core Course, Session 18 .NET Core, ASP.NET Core Course, Session 18
.NET Core, ASP.NET Core Course, Session 18
aminmesbahi
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Spring aop
Spring aopSpring aop
Spring aop
Hamid Ghorbani
 
Hibernate Interview Questions
Hibernate Interview QuestionsHibernate Interview Questions
Hibernate Interview QuestionsSyed Shahul
 
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Spring Web Service, Spring JMS, Eclipse & Maven tutorialsSpring Web Service, Spring JMS, Eclipse & Maven tutorials
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Raghavan Mohan
 

What's hot (20)

AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101
 
Maven
MavenMaven
Maven
 
Struts notes
Struts notesStruts notes
Struts notes
 
Leverage Hibernate and Spring Features Together
Leverage Hibernate and Spring Features TogetherLeverage Hibernate and Spring Features Together
Leverage Hibernate and Spring Features Together
 
Exploring Maven SVN GIT
Exploring Maven SVN GITExploring Maven SVN GIT
Exploring Maven SVN GIT
 
Working with Servlets
Working with ServletsWorking with Servlets
Working with Servlets
 
Spring Framework -I
Spring Framework -ISpring Framework -I
Spring Framework -I
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed intro
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)
 
.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14
 
.NET Core, ASP.NET Core Course, Session 18
 .NET Core, ASP.NET Core Course, Session 18 .NET Core, ASP.NET Core Course, Session 18
.NET Core, ASP.NET Core Course, Session 18
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
Spring aop
Spring aopSpring aop
Spring aop
 
Hibernate Interview Questions
Hibernate Interview QuestionsHibernate Interview Questions
Hibernate Interview Questions
 
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Spring Web Service, Spring JMS, Eclipse & Maven tutorialsSpring Web Service, Spring JMS, Eclipse & Maven tutorials
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
 
IoC with PHP
IoC with PHPIoC with PHP
IoC with PHP
 

Viewers also liked

Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
Sunil kumar Mohanty
 
Dependency Injection in Spring in 10min
Dependency Injection in Spring in 10minDependency Injection in Spring in 10min
Dependency Injection in Spring in 10minCorneil du Plessis
 
Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injection
Steve Ng
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injectionsrmelody
 
Types of containers
Types of  containersTypes of  containers
Types of containers
Sagar Gadhiya(PaTel)
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
Types of containers
Types of containers Types of containers
Types of containers
MAX GALARZA HERNANDEZ
 
A Brief presentation on Containerisation
A Brief presentation on ContainerisationA Brief presentation on Containerisation
A Brief presentation on Containerisation
subhash_ae
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)
Stormpath
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-SideJava EE and Spring Side-by-Side
Java EE and Spring Side-by-Side
Reza Rahman
 

Viewers also liked (11)

Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
Dependency Injection in Spring in 10min
Dependency Injection in Spring in 10minDependency Injection in Spring in 10min
Dependency Injection in Spring in 10min
 
Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injection
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injection
 
Types of containers
Types of  containersTypes of  containers
Types of containers
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Types of containers
Types of containers Types of containers
Types of containers
 
A Brief presentation on Containerisation
A Brief presentation on ContainerisationA Brief presentation on Containerisation
A Brief presentation on Containerisation
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)Secure Your REST API (The Right Way)
Secure Your REST API (The Right Way)
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-SideJava EE and Spring Side-by-Side
Java EE and Spring Side-by-Side
 

Similar to Spring IOC advantages and developing spring application sample

Spring Basics
Spring BasicsSpring Basics
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf
DeoDuaNaoHet
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
Aneega
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questionsDhiraj Champawat
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Rajind Ruparathna
 
Spring framework
Spring frameworkSpring framework
Spring framework
Rajkumar Singh
 
Spring Fa Qs
Spring Fa QsSpring Fa Qs
Spring Fa Qsjbashask
 
Spring
SpringSpring
Spring
gauravashq
 
Spring
SpringSpring
Spring
gauravashq
 
Spring
SpringSpring
Spring
gauravashq
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkAkhil Mittal
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
Mindsmapped Consulting
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2
javatrainingonline
 
Spring Framework - III
Spring Framework - IIISpring Framework - III
Spring Framework - III
People Strategists
 
Spring framework IOC and Dependency Injection
Spring framework  IOC and Dependency InjectionSpring framework  IOC and Dependency Injection
Spring framework IOC and Dependency Injection
Anuj Singh Rajput
 
Tomcat + other things
Tomcat + other thingsTomcat + other things
Tomcat + other things
Aravindharamanan S
 
Spring training
Spring trainingSpring training
Spring trainingshah_d_p
 
Spring framework
Spring frameworkSpring framework
Spring framework
vietduc17
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 

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

Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Spring Fa Qs
Spring Fa QsSpring Fa Qs
Spring Fa Qs
 
Spring
SpringSpring
Spring
 
Spring
SpringSpring
Spring
 
Spring
SpringSpring
Spring
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2
 
Spring Framework - III
Spring Framework - IIISpring Framework - III
Spring Framework - III
 
Spring framework IOC and Dependency Injection
Spring framework  IOC and Dependency InjectionSpring framework  IOC and Dependency Injection
Spring framework IOC and Dependency Injection
 
Tomcat + other things
Tomcat + other thingsTomcat + other things
Tomcat + other things
 
Spring training
Spring trainingSpring training
Spring training
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 

Recently uploaded

FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 

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