SlideShare a Scribd company logo
1 of 17
Spring Framework
Sonal Poddar
sonalpoddar@hotmail.com
Content
• What is Spring Framework?
• Key features of Spring Framework
 Spring Modules
 Dependency Injection and Inversion of Control
 Spring AOP
• Spring Environment Setup
• Quick Steps to Develop Spring Application
• Spring Hello World Example
• Setter Injection v/s Constructor Injection
• Spring Autowiring
What is Spring Framework?
• Spring is a light weight and open source framework created
by Rod Johnson in 2003
• It is a framework of frameworks
• Spring framework is said to be a non-invasive.
• Spring Framework made J2EE application development little
easier, by introducing POJO model
• Spring having so much of demand because of the following 3
reasons….
 Simplicity
 Testability
 Loose Coupling
Key features of Spring Framework
Spring Modules
 Test
 Core Container
 AOP
 Aspects
 Instrumentation
 Data Access/ Integration
 Web (MVC / Remoting)
Spring Core Module – Loose Coupling
Tight Coupling
When an object creates the object to be used, then it is a tight coupling
situation.
As the main object creates the object itself, this object can not be changed
from outside world easily marked it as tightly coupled objects.
When an object gets the object to be used from the outside, then it is a loose
coupling situation.
As the main object is merely using the object, this object can be changed
from the outside world easily marked it as loosely coupled objects.
Loose Coupling
IoC and Dependency Injection
What is Inversion of Control ?
Inversion of Control is a principle in software engineering by which the control
of objects or portions of a program is transferred to a container or framework.
It's most often used in the context of object-oriented programming.
Inversion of Control can be achieved through various mechanisms such as:
Strategy design pattern, Service Locator pattern, Factory pattern, and
Dependency Injection (DI).
IoC and Dependency Injection (cont.)
What is Spring IoC Container ?
• An IoC container is a common characteristic of frameworks that implement
IoC.
• In the Spring framework, the IoC container is represented by the interface
ApplicationContext.
• The Spring container is responsible for instantiating, configuring and
assembling objects known as beans, as well as managing their lifecycle.
• The Spring framework provides several implementations of the
ApplicationContext interface — ClassPathXmlApplicationContext and
FileSystemXmlApplicationContext for standalone applications, and
WebApplicationContext for web applications.
• Here's one way to manually instantiate a container:
IoC and Dependency Injection (cont.)
Spring IoC Container Overview
• The configuration metadata can be represented either by;
 XML
 Java Annotations
IoC and Dependency Injection (cont.)
What is Dependency Injection?
Dependency injection is a pattern through which to implement IoC, where the
control being inverted is the setting of object's dependencies.
The act of connecting objects with other objects, or “injecting” objects into
other objects, is done by an assembler rather than by the objects themselves.
Traditional Programming Using DI
IoC and Dependency Injection (cont.)
Types of Dependency Injection
Spring framework provides two ways to inject dependency:-
 By Setter method
• In this type of injection, the spring container uses setter method in the
dependent class for injecting its dependencies (primitive values or any).
• Spring container knows whether to perform setter or constructor
injection by reading the information from an external file called spring
configuration file
• In spring config xml, we need to inform to the spring IOC container about
setter injection by using <property/>
 By Constructor
• In this type of injection, spring container uses constructor of the bean
class for assigning the dependencies.
• In spring config xml, we need to inform to the spring IOC container about
constructor injection by using <constructor –arg/>
Spring AOP
Introduction
• Aspect Oriented Programming (AOP) compliments OOPs in the sense that
it also provides modularity. But the key unit of modularity is aspect than
class.
• AOP breaks the program logic into distinct parts (called concerns). It is
used to increase modularity by cross-cutting concerns.
• A cross-cutting concern is a concern that can affect the whole application
and should be centralized in one location in code as possible, such as
transaction management, authentication, logging, security etc.
• It provides the pluggable way to dynamically add the additional concern
before, after or around the actual logic.
• AOP terminologies are Join point, Advice, Pointcut, Introduction,
Target Object, Aspect, Interceptor, AOP Proxy, Weaving
Spring Environment Setup
 Step 1 - Setup Java Development Kit (JDK)
You can download the latest version of SDK from Oracle's Java site
https://www.oracle.com/java/technologies/javase-downloads.html
 Step 2 - Install Apache Common Logging API
You can download the latest version of Apache Commons Logging API from
https://commons.apache.org/logging/
 Step 3 - Setup Eclipse IDE
To install Eclipse IDE, download the latest Eclipse binaries from
https://www.eclipse.org/downloads/
 Step 4 - Setup Spring Framework Libraries
Download the latest version of Spring framework libraries from
https://repo.spring.io/release/org/springframework/spring
Quick Steps to Develop Spring Application
• Spring environment starts by loading spring configuration xml file into Resource
Interface object.
• Actually this configuration file contains all bean configurations about our
application [ something like struts.xml in Struts 2 ]
• ClassPathResource is the implementation class of the Resource interface.
• Spring IOC container is called BeanFactory and this container is responsible for
creating the bean objects and for injecting it’s dependencies throughout our
applications.
• The XmlBeanFactory is the implementation class of the BeanFactory. There are
many methods in the BeanFactory interface. One method is getBean(), which
returns the object of the associated class.
• Syntax
Resource res = new ClassPathResource(“Our Config xml file name”);
BeanFactory factory = new XmlBeanFactory(res);
Spring Hello World Example
Demo in Eclipse
Using Setter Injection & Constructor Injection with Primitive Values
Setter Injection v/s Constructor Injection
 Partial dependency: can be injected using setter injection but it is not
possible by constructor. Suppose there are 3 properties in a class, having 3 arg
constructor and setters methods. In such case, if you want to pass information
for only one property, it is possible by setter method only.
 Overriding: Setter injection overrides the constructor injection. If we use
both constructor and setter injection, IOC container will use the setter
injection.
 Changes: We can easily change the value by setter injection. It doesn't
create a new bean instance always like constructor. So setter injection is
flexible than constructor injection.
Spring Autowiring
 Autowiring feature of spring framework enables you to inject the object
dependency implicitly.
 It internally uses setter or constructor injection.
 Autowiring can't be used to inject primitive values. It works with
reference only.
Autowiring Modes
• byName
• byType
• constructor
• autoDetect
• none
Thank You!

More Related Content

What's hot

What's hot (20)

Spring boot
Spring bootSpring boot
Spring boot
 
Introduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoCIntroduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoC
 
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
 
Packages
PackagesPackages
Packages
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Springboot introduction
Springboot introductionSpringboot introduction
Springboot introduction
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Spring boot
Spring bootSpring boot
Spring boot
 
J2ee
J2eeJ2ee
J2ee
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Dmytro Patkovskyi "Practical tips regarding build optimization for those who ...
Dmytro Patkovskyi "Practical tips regarding build optimization for those who ...Dmytro Patkovskyi "Practical tips regarding build optimization for those who ...
Dmytro Patkovskyi "Practical tips regarding build optimization for those who ...
 
Using Mockito
Using MockitoUsing Mockito
Using Mockito
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 

Similar to Spring framework

Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to Spring
Sujit Kumar
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptx
NourhanTarek23
 
Spring framework
Spring frameworkSpring framework
Spring framework
Kani Selvam
 

Similar to Spring framework (20)

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to Spring
 
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
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
 
Spring session
Spring sessionSpring session
Spring session
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptx
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAO
 
Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
 
스프링 프레임워크
스프링 프레임워크스프링 프레임워크
스프링 프레임워크
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
 
Springs_Training
Springs_TrainingSprings_Training
Springs_Training
 
Java spring framework
Java spring frameworkJava spring framework
Java spring framework
 
Java spring ppt
Java spring pptJava spring ppt
Java spring ppt
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Spring Framework Presantation Part 1-Core
Spring Framework Presantation Part 1-CoreSpring Framework Presantation Part 1-Core
Spring Framework Presantation Part 1-Core
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Spring framework

  • 2. Content • What is Spring Framework? • Key features of Spring Framework  Spring Modules  Dependency Injection and Inversion of Control  Spring AOP • Spring Environment Setup • Quick Steps to Develop Spring Application • Spring Hello World Example • Setter Injection v/s Constructor Injection • Spring Autowiring
  • 3. What is Spring Framework? • Spring is a light weight and open source framework created by Rod Johnson in 2003 • It is a framework of frameworks • Spring framework is said to be a non-invasive. • Spring Framework made J2EE application development little easier, by introducing POJO model • Spring having so much of demand because of the following 3 reasons….  Simplicity  Testability  Loose Coupling
  • 4. Key features of Spring Framework Spring Modules  Test  Core Container  AOP  Aspects  Instrumentation  Data Access/ Integration  Web (MVC / Remoting)
  • 5. Spring Core Module – Loose Coupling Tight Coupling When an object creates the object to be used, then it is a tight coupling situation. As the main object creates the object itself, this object can not be changed from outside world easily marked it as tightly coupled objects. When an object gets the object to be used from the outside, then it is a loose coupling situation. As the main object is merely using the object, this object can be changed from the outside world easily marked it as loosely coupled objects. Loose Coupling
  • 6. IoC and Dependency Injection What is Inversion of Control ? Inversion of Control is a principle in software engineering by which the control of objects or portions of a program is transferred to a container or framework. It's most often used in the context of object-oriented programming. Inversion of Control can be achieved through various mechanisms such as: Strategy design pattern, Service Locator pattern, Factory pattern, and Dependency Injection (DI).
  • 7. IoC and Dependency Injection (cont.) What is Spring IoC Container ? • An IoC container is a common characteristic of frameworks that implement IoC. • In the Spring framework, the IoC container is represented by the interface ApplicationContext. • The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their lifecycle. • The Spring framework provides several implementations of the ApplicationContext interface — ClassPathXmlApplicationContext and FileSystemXmlApplicationContext for standalone applications, and WebApplicationContext for web applications. • Here's one way to manually instantiate a container:
  • 8. IoC and Dependency Injection (cont.) Spring IoC Container Overview • The configuration metadata can be represented either by;  XML  Java Annotations
  • 9. IoC and Dependency Injection (cont.) What is Dependency Injection? Dependency injection is a pattern through which to implement IoC, where the control being inverted is the setting of object's dependencies. The act of connecting objects with other objects, or “injecting” objects into other objects, is done by an assembler rather than by the objects themselves. Traditional Programming Using DI
  • 10. IoC and Dependency Injection (cont.) Types of Dependency Injection Spring framework provides two ways to inject dependency:-  By Setter method • In this type of injection, the spring container uses setter method in the dependent class for injecting its dependencies (primitive values or any). • Spring container knows whether to perform setter or constructor injection by reading the information from an external file called spring configuration file • In spring config xml, we need to inform to the spring IOC container about setter injection by using <property/>  By Constructor • In this type of injection, spring container uses constructor of the bean class for assigning the dependencies. • In spring config xml, we need to inform to the spring IOC container about constructor injection by using <constructor –arg/>
  • 11. Spring AOP Introduction • Aspect Oriented Programming (AOP) compliments OOPs in the sense that it also provides modularity. But the key unit of modularity is aspect than class. • AOP breaks the program logic into distinct parts (called concerns). It is used to increase modularity by cross-cutting concerns. • A cross-cutting concern is a concern that can affect the whole application and should be centralized in one location in code as possible, such as transaction management, authentication, logging, security etc. • It provides the pluggable way to dynamically add the additional concern before, after or around the actual logic. • AOP terminologies are Join point, Advice, Pointcut, Introduction, Target Object, Aspect, Interceptor, AOP Proxy, Weaving
  • 12. Spring Environment Setup  Step 1 - Setup Java Development Kit (JDK) You can download the latest version of SDK from Oracle's Java site https://www.oracle.com/java/technologies/javase-downloads.html  Step 2 - Install Apache Common Logging API You can download the latest version of Apache Commons Logging API from https://commons.apache.org/logging/  Step 3 - Setup Eclipse IDE To install Eclipse IDE, download the latest Eclipse binaries from https://www.eclipse.org/downloads/  Step 4 - Setup Spring Framework Libraries Download the latest version of Spring framework libraries from https://repo.spring.io/release/org/springframework/spring
  • 13. Quick Steps to Develop Spring Application • Spring environment starts by loading spring configuration xml file into Resource Interface object. • Actually this configuration file contains all bean configurations about our application [ something like struts.xml in Struts 2 ] • ClassPathResource is the implementation class of the Resource interface. • Spring IOC container is called BeanFactory and this container is responsible for creating the bean objects and for injecting it’s dependencies throughout our applications. • The XmlBeanFactory is the implementation class of the BeanFactory. There are many methods in the BeanFactory interface. One method is getBean(), which returns the object of the associated class. • Syntax Resource res = new ClassPathResource(“Our Config xml file name”); BeanFactory factory = new XmlBeanFactory(res);
  • 14. Spring Hello World Example Demo in Eclipse Using Setter Injection & Constructor Injection with Primitive Values
  • 15. Setter Injection v/s Constructor Injection  Partial dependency: can be injected using setter injection but it is not possible by constructor. Suppose there are 3 properties in a class, having 3 arg constructor and setters methods. In such case, if you want to pass information for only one property, it is possible by setter method only.  Overriding: Setter injection overrides the constructor injection. If we use both constructor and setter injection, IOC container will use the setter injection.  Changes: We can easily change the value by setter injection. It doesn't create a new bean instance always like constructor. So setter injection is flexible than constructor injection.
  • 16. Spring Autowiring  Autowiring feature of spring framework enables you to inject the object dependency implicitly.  It internally uses setter or constructor injection.  Autowiring can't be used to inject primitive values. It works with reference only. Autowiring Modes • byName • byType • constructor • autoDetect • none