SlideShare a Scribd company logo
Dependency Injection
With Spring
Presenter: Pranav Kumar, Mindfire Solutions
Agenda
 What is Dependency Injection ?
 Dependency Inversion Principle
 Types of Dependency Injection
 Why DI ?
 Understand DI with StudentSystem Example
 DI with Spring Configuration
 Benefits of DI
 DI Using Annotations
Presenter: Pranav Kumar, Mindfire Solutions
DI: What it is ?
 Dependency Injection is a software design pattern that allows removing hard
coded dependencies and making it possible to change them at run-time.
 Dependencies are injected into objects by a container.
 Also Known as Inversion Of Control (IoC).
Presenter: Pranav Kumar, Mindfire Solutions
DI Principle
 The dependency inversion principle was postulated by Robert C. Martin
 The principle states:
• Always use abstractions (Interface)
• Objects should define their dependencies only through
 Constructor arguments
 Arguments to a factory method.
 Properties which are set on the object instance
Presenter: Pranav Kumar, Mindfire Solutions
DI: Types
 Setter injection - Dependent module exposes a setter method that the framework uses to
inject the dependency.
 Constructor injection - Dependencies are provided through the class constructor.
Presenter: Pranav Kumar, Mindfire Solutions
DI: Why ?
 Dependency Injection enables 'POJO programming'
 POJO programming facilitate
- Simplicity
- Effective separation of concerns
- Proper unit testing
 Well-factored and well-tested code
- Tends to be well-designed code
- Evolves well into maintainable systems
Presenter: Pranav Kumar, Mindfire Solutions
Example: The Student System
 To improve your skills in Java development, you decide to develop a student system.
 You decide to use a file to store student information.
 You create a class FileStudentDAO responsible for writing and reading to the file
 You create a class StudentSystem responsible for performing the logic of the system.
 You’ve learned that it’s a good thing to program to interfaces
Presenter: Pranav Kumar, Mindfire Solutions
DI:
Presenter: Pranav Kumar, Mindfire Solutions
The DefaultStudentSystem
Presenter: Pranav Kumar, Mindfire Solutions
Works! Or .. ?
The system is a big success – University of Oslo wants to adopt it!
Presenter: Pranav Kumar, Mindfire Solutions
Works! Or .. ?
You make a new implementation of the StudentDAO for University of Oslo, a
MySQLStudentDAO:
Presenter: Pranav Kumar, Mindfire Solutions
Works! Or .. ?
 Won’t work! The FileStudentDAO is hard-coded into the StudentSystem:
 The DefaultStudentSystem implementation is responsible for obtaining a StudentDAO.
 Dependent both on the StudentDAO interface and the implementation
Presenter: Pranav Kumar, Mindfire Solutions
public class DefaultStudentSystem implements StudentSystem {
private StudentDAO studentDAO = new FileStudentDAO();
...
Problems: Student System
How to deploy the StudentSystem at different locations?
 Develop various versions for each location?
• Time consuming
• Confusing and error-prone
• Requires more efforts for versioning
Solution:
 Use Dependency Injection!
• More specific term derived from the term Inversion of Control
Presenter: Pranav Kumar, Mindfire Solutions
Traditional Vs DI
Presenter: Pranav Kumar, Mindfire Solutions
DI: With Spring
 Bean: A class that is managed by a Spring IoC container
 Setter based DI: Provide a public set-method for the dependency reference.
Presenter: Pranav Kumar, Mindfire Solutions
DI: Spring Configuration
 Configuration: How to instantiate, configure, and assemble the objects in your application
• The Spring container accepts many configuration formats
• XML based configuration most common.
Presenter: Pranav Kumar, Mindfire Solutions
DI: Spring Configuration
Presenter: Pranav Kumar, Mindfire Solutions
studentDAO
Injected into
studentSystem
Refers to the
studentDAO
bean
Refers to the
studentDAO attribute in Java
class
DI: Spring Configuration
Presenter: Pranav Kumar, Mindfire Solutions
Summary
Presenter: Pranav Kumar, Mindfire Solutions
DI: Benefits
Presenter: Pranav Kumar, Mindfire Solutions
 Easier to swap implementations of dependencies
 The system can be re-configured without changing the compiled code
 Easier testing
 Improved re-usability of your classes
 Cleaner code
DI: Using Annotations
Presenter: Pranav Kumar, Mindfire Solutions
 Annotation is a meta-tag – applied to classes, methods...
• Associated with program behaviour
 @Component
• Defines a class as a Spring container managed component
 @Autowired
• Tells Spring to inject a component
 Classpath scanning for components
• <context:component-scan base-package="org.example"/>
DI: Using Annotations
Presenter: Pranav Kumar, Mindfire Solutions
@Component
public class StudentService
{
@Autowired
StudentDAO studentDAO;
//constructors
//Getter setter methods
}
@Component
public class StudentDAO
{
@Override
public String toString() {
return "This is StudentDAO";
}
}
StudentService.java StudentDAO.java
DI: Using Annotations
<bean id="studentDAO" class="com.mindfire.spring.student.dao.StudentDAO"/>

<bean id="studentService" class="com.mindfire.spring.student.service.StudentService">
<property name="studentDAO" ref="studentDAO" /> 
<constructor-arg ref="studentDAO"> </constructor-arg> 
</bean>

<context:component-scan base-package="com.mindfire.spring.student" />
Presenter: Pranav Kumar, Mindfire Solutions
Annotation configuration
XML Configuration
Resources
Presenter: Pranav Kumar, Mindfire Solutions
 Books
• Rod Johnson, Juergen Hoeller: Expert One-on-One J2EE
• Craig Walls and Ryan Breidenbach: Spring in Action
 The Spring reference documentation
• www.springframework.org
Questions ?
Presenter: Pranav Kumar, Mindfire Solutions
Presenter: Pranav Kumar, Mindfire Solutions

More Related Content

What's hot

Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti
 
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...
Edureka!
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
Cuelogic Technologies Pvt. Ltd.
 
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Edureka!
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
Levon Apreyan
 
Spring boot
Spring bootSpring boot
Spring boot
Pradeep Shanmugam
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Hùng Nguyễn Huy
 
Java Collections
Java  Collections Java  Collections
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with selenium
Tzirla Rozental
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
Santosh Kumar Kar
 
Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
Return on Intelligence
 
A Java Microservices Spring Boot and Docker case study.
A Java Microservices Spring Boot and Docker case study.A Java Microservices Spring Boot and Docker case study.
A Java Microservices Spring Boot and Docker case study.
Subramanyam Vemala
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WS
Katrien Verbert
 
Core java
Core javaCore java
Core java
Sun Technlogies
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
Hùng Nguyễn Huy
 
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 Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 

What's hot (20)

Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
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...
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
 
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Spring boot
Spring bootSpring boot
Spring boot
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with selenium
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
 
A Java Microservices Spring Boot and Docker case study.
A Java Microservices Spring Boot and Docker case study.A Java Microservices Spring Boot and Docker case study.
A Java Microservices Spring Boot and Docker case study.
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WS
 
Core java
Core javaCore java
Core java
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
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 Boot
Spring BootSpring Boot
Spring Boot
 

Viewers also liked

FrontEnd platform based on AngularJS
FrontEnd platform based on AngularJSFrontEnd platform based on AngularJS
FrontEnd platform based on AngularJS
Egor Miasnikov
 
Building Modern Web Apps with AngularJS
Building Modern Web Apps with AngularJSBuilding Modern Web Apps with AngularJS
Building Modern Web Apps with AngularJS
Raveen Perera
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
Sathish VJ
 
Angular 1.5 Components
Angular 1.5 ComponentsAngular 1.5 Components
Angular 1.5 Components
José Barbosa
 
Scope demystified - AngularJS
Scope demystified - AngularJSScope demystified - AngularJS
Scope demystified - AngularJS
Sumanth krishna
 
Creating GUI Component APIs in Angular and Web Components
Creating GUI Component APIs in Angular and Web ComponentsCreating GUI Component APIs in Angular and Web Components
Creating GUI Component APIs in Angular and Web Components
Rachael L Moore
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
murtazahaveliwala
 

Viewers also liked (7)

FrontEnd platform based on AngularJS
FrontEnd platform based on AngularJSFrontEnd platform based on AngularJS
FrontEnd platform based on AngularJS
 
Building Modern Web Apps with AngularJS
Building Modern Web Apps with AngularJSBuilding Modern Web Apps with AngularJS
Building Modern Web Apps with AngularJS
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 
Angular 1.5 Components
Angular 1.5 ComponentsAngular 1.5 Components
Angular 1.5 Components
 
Scope demystified - AngularJS
Scope demystified - AngularJSScope demystified - AngularJS
Scope demystified - AngularJS
 
Creating GUI Component APIs in Angular and Web Components
Creating GUI Component APIs in Angular and Web ComponentsCreating GUI Component APIs in Angular and Web Components
Creating GUI Component APIs in Angular and Web Components
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
 

Similar to Dependency injection

66781291 java-lab-manual
66781291 java-lab-manual66781291 java-lab-manual
66781291 java-lab-manual
Laura Popovici
 
Ujjwalreverseengineeringppptfinal
UjjwalreverseengineeringppptfinalUjjwalreverseengineeringppptfinal
Ujjwalreverseengineeringppptfinal
ujjwalchauhan87
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
Knoldus Inc.
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
anshu_atri
 
Presentation5
Presentation5Presentation5
Presentation5
Natasha Bains
 
Corejavacoursesyllabus 140226051356-phpapp01
Corejavacoursesyllabus 140226051356-phpapp01Corejavacoursesyllabus 140226051356-phpapp01
Corejavacoursesyllabus 140226051356-phpapp01
Sandeep Vishwakarma
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
mrxyz19
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
shyaminfo16
 
Core java course syllabus
Core java course syllabusCore java course syllabus
Core java course syllabus
Papitha Velumani
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
dixonbakerr
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
shyaminfo40
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
charlesangles123
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
chanduruc123
 
Maven 2 features
Maven 2 featuresMaven 2 features
Maven 2 features
Angel Ruiz
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
uopassignment
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
lroselyn
 
Vikeshp
VikeshpVikeshp
Vikeshp
MdAsu1
 
Creating MVC Application with backbone js
Creating MVC Application with backbone jsCreating MVC Application with backbone js
Creating MVC Application with backbone js
Mindfire Solutions
 
CAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEMCAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEM
Abhishek Shakya
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mocking
mrjawright
 

Similar to Dependency injection (20)

66781291 java-lab-manual
66781291 java-lab-manual66781291 java-lab-manual
66781291 java-lab-manual
 
Ujjwalreverseengineeringppptfinal
UjjwalreverseengineeringppptfinalUjjwalreverseengineeringppptfinal
Ujjwalreverseengineeringppptfinal
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
 
Presentation5
Presentation5Presentation5
Presentation5
 
Corejavacoursesyllabus 140226051356-phpapp01
Corejavacoursesyllabus 140226051356-phpapp01Corejavacoursesyllabus 140226051356-phpapp01
Corejavacoursesyllabus 140226051356-phpapp01
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
 
Core java course syllabus
Core java course syllabusCore java course syllabus
Core java course syllabus
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
 
Maven 2 features
Maven 2 featuresMaven 2 features
Maven 2 features
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
 
Strayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade newStrayer cis 406 week 10 assignment 2 u grade new
Strayer cis 406 week 10 assignment 2 u grade new
 
Vikeshp
VikeshpVikeshp
Vikeshp
 
Creating MVC Application with backbone js
Creating MVC Application with backbone jsCreating MVC Application with backbone js
Creating MVC Application with backbone js
 
CAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEMCAR SHOWROOM SYSTEM
CAR SHOWROOM SYSTEM
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mocking
 

More from Mindfire Solutions

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
Mindfire Solutions
 
diet management app
diet management appdiet management app
diet management app
Mindfire Solutions
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
Mindfire Solutions
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
Mindfire Solutions
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
Mindfire Solutions
 
ELMAH
ELMAHELMAH
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
Mindfire Solutions
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
Mindfire Solutions
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
Mindfire Solutions
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
Mindfire Solutions
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
Mindfire Solutions
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
Mindfire Solutions
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
Mindfire Solutions
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
Mindfire Solutions
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
Mindfire Solutions
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
Mindfire Solutions
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
Mindfire Solutions
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
Mindfire Solutions
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
Mindfire Solutions
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
Mindfire Solutions
 

More from Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 

Recently uploaded

FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptxFIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Alliance
 
What's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptxWhat's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptx
Stephanie Beckett
 
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
Fwdays
 
The Challenge of Interpretability in Generative AI Models.pdf
The Challenge of Interpretability in Generative AI Models.pdfThe Challenge of Interpretability in Generative AI Models.pdf
The Challenge of Interpretability in Generative AI Models.pdf
Sara Kroft
 
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise ExcellenceCracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Quentin Reul
 
The Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - CoatueThe Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - Coatue
Razin Mustafiz
 
Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...
Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...
Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...
Snarky Security
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Alliance
 
DefCamp_2016_Chemerkin_Yury_--_publish.pdf
DefCamp_2016_Chemerkin_Yury_--_publish.pdfDefCamp_2016_Chemerkin_Yury_--_publish.pdf
DefCamp_2016_Chemerkin_Yury_--_publish.pdf
Yury Chemerkin
 
Indian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for StartupsIndian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for Startups
AMol NAik
 
Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...
Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...
Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...
OnBoard
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
siddu769252
 
Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+
Zilliz
 
Keynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive SecurityKeynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive Security
Priyanka Aash
 
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptxFIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Alliance
 
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
Fwdays
 
Finetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and DefendingFinetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and Defending
Priyanka Aash
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
Zilliz
 
How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...
DianaGray10
 
Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17
Bhajan Mehta
 

Recently uploaded (20)

FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptxFIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptx
 
What's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptxWhat's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptx
 
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx"Making .NET Application Even Faster", Sergey Teplyakov.pptx
"Making .NET Application Even Faster", Sergey Teplyakov.pptx
 
The Challenge of Interpretability in Generative AI Models.pdf
The Challenge of Interpretability in Generative AI Models.pdfThe Challenge of Interpretability in Generative AI Models.pdf
The Challenge of Interpretability in Generative AI Models.pdf
 
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise ExcellenceCracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
 
The Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - CoatueThe Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - Coatue
 
Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...
Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...
Welcome to Cyberbiosecurity. Because regular cybersecurity wasn't complicated...
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
 
DefCamp_2016_Chemerkin_Yury_--_publish.pdf
DefCamp_2016_Chemerkin_Yury_--_publish.pdfDefCamp_2016_Chemerkin_Yury_--_publish.pdf
DefCamp_2016_Chemerkin_Yury_--_publish.pdf
 
Indian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for StartupsIndian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for Startups
 
Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...
Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...
Mastering Board Best Practices: Essential Skills for Effective Non-profit Lea...
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
 
Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+
 
Keynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive SecurityKeynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive Security
 
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptxFIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
 
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
 
Finetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and DefendingFinetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and Defending
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
 
How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...
 
Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17
 

Dependency injection

  • 1. Dependency Injection With Spring Presenter: Pranav Kumar, Mindfire Solutions
  • 2. Agenda  What is Dependency Injection ?  Dependency Inversion Principle  Types of Dependency Injection  Why DI ?  Understand DI with StudentSystem Example  DI with Spring Configuration  Benefits of DI  DI Using Annotations Presenter: Pranav Kumar, Mindfire Solutions
  • 3. DI: What it is ?  Dependency Injection is a software design pattern that allows removing hard coded dependencies and making it possible to change them at run-time.  Dependencies are injected into objects by a container.  Also Known as Inversion Of Control (IoC). Presenter: Pranav Kumar, Mindfire Solutions
  • 4. DI Principle  The dependency inversion principle was postulated by Robert C. Martin  The principle states: • Always use abstractions (Interface) • Objects should define their dependencies only through  Constructor arguments  Arguments to a factory method.  Properties which are set on the object instance Presenter: Pranav Kumar, Mindfire Solutions
  • 5. DI: Types  Setter injection - Dependent module exposes a setter method that the framework uses to inject the dependency.  Constructor injection - Dependencies are provided through the class constructor. Presenter: Pranav Kumar, Mindfire Solutions
  • 6. DI: Why ?  Dependency Injection enables 'POJO programming'  POJO programming facilitate - Simplicity - Effective separation of concerns - Proper unit testing  Well-factored and well-tested code - Tends to be well-designed code - Evolves well into maintainable systems Presenter: Pranav Kumar, Mindfire Solutions
  • 7. Example: The Student System  To improve your skills in Java development, you decide to develop a student system.  You decide to use a file to store student information.  You create a class FileStudentDAO responsible for writing and reading to the file  You create a class StudentSystem responsible for performing the logic of the system.  You’ve learned that it’s a good thing to program to interfaces Presenter: Pranav Kumar, Mindfire Solutions
  • 8. DI: Presenter: Pranav Kumar, Mindfire Solutions
  • 9. The DefaultStudentSystem Presenter: Pranav Kumar, Mindfire Solutions
  • 10. Works! Or .. ? The system is a big success – University of Oslo wants to adopt it! Presenter: Pranav Kumar, Mindfire Solutions
  • 11. Works! Or .. ? You make a new implementation of the StudentDAO for University of Oslo, a MySQLStudentDAO: Presenter: Pranav Kumar, Mindfire Solutions
  • 12. Works! Or .. ?  Won’t work! The FileStudentDAO is hard-coded into the StudentSystem:  The DefaultStudentSystem implementation is responsible for obtaining a StudentDAO.  Dependent both on the StudentDAO interface and the implementation Presenter: Pranav Kumar, Mindfire Solutions public class DefaultStudentSystem implements StudentSystem { private StudentDAO studentDAO = new FileStudentDAO(); ...
  • 13. Problems: Student System How to deploy the StudentSystem at different locations?  Develop various versions for each location? • Time consuming • Confusing and error-prone • Requires more efforts for versioning Solution:  Use Dependency Injection! • More specific term derived from the term Inversion of Control Presenter: Pranav Kumar, Mindfire Solutions
  • 14. Traditional Vs DI Presenter: Pranav Kumar, Mindfire Solutions
  • 15. DI: With Spring  Bean: A class that is managed by a Spring IoC container  Setter based DI: Provide a public set-method for the dependency reference. Presenter: Pranav Kumar, Mindfire Solutions
  • 16. DI: Spring Configuration  Configuration: How to instantiate, configure, and assemble the objects in your application • The Spring container accepts many configuration formats • XML based configuration most common. Presenter: Pranav Kumar, Mindfire Solutions
  • 17. DI: Spring Configuration Presenter: Pranav Kumar, Mindfire Solutions studentDAO Injected into studentSystem Refers to the studentDAO bean Refers to the studentDAO attribute in Java class
  • 18. DI: Spring Configuration Presenter: Pranav Kumar, Mindfire Solutions
  • 19. Summary Presenter: Pranav Kumar, Mindfire Solutions
  • 20. DI: Benefits Presenter: Pranav Kumar, Mindfire Solutions  Easier to swap implementations of dependencies  The system can be re-configured without changing the compiled code  Easier testing  Improved re-usability of your classes  Cleaner code
  • 21. DI: Using Annotations Presenter: Pranav Kumar, Mindfire Solutions  Annotation is a meta-tag – applied to classes, methods... • Associated with program behaviour  @Component • Defines a class as a Spring container managed component  @Autowired • Tells Spring to inject a component  Classpath scanning for components • <context:component-scan base-package="org.example"/>
  • 22. DI: Using Annotations Presenter: Pranav Kumar, Mindfire Solutions @Component public class StudentService { @Autowired StudentDAO studentDAO; //constructors //Getter setter methods } @Component public class StudentDAO { @Override public String toString() { return "This is StudentDAO"; } } StudentService.java StudentDAO.java
  • 23. DI: Using Annotations <bean id="studentDAO" class="com.mindfire.spring.student.dao.StudentDAO"/> <!-- Injecting studentDAO to StudentService via xml configuration --> <bean id="studentService" class="com.mindfire.spring.student.service.StudentService"> <property name="studentDAO" ref="studentDAO" /> <!-- Setter injection --> <constructor-arg ref="studentDAO"> </constructor-arg> <!-- Constructor injection --> </bean> <!-- This is used to auto scanning of beans annotated by @Component. --> <context:component-scan base-package="com.mindfire.spring.student" /> Presenter: Pranav Kumar, Mindfire Solutions Annotation configuration XML Configuration
  • 24. Resources Presenter: Pranav Kumar, Mindfire Solutions  Books • Rod Johnson, Juergen Hoeller: Expert One-on-One J2EE • Craig Walls and Ryan Breidenbach: Spring in Action  The Spring reference documentation • www.springframework.org
  • 25. Questions ? Presenter: Pranav Kumar, Mindfire Solutions
  • 26. Presenter: Pranav Kumar, Mindfire Solutions