SlideShare a Scribd company logo
1 of 26
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"/>
<!-- 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
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 (20)

Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Dependency injection ppt
Dependency injection pptDependency injection ppt
Dependency injection ppt
 
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
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Spring boot
Spring bootSpring boot
Spring boot
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
 
Introduction to Spring's Dependency Injection
Introduction to Spring's Dependency InjectionIntroduction to Spring's Dependency Injection
Introduction to Spring's Dependency Injection
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Java reflection
Java reflectionJava reflection
Java reflection
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 

Viewers also liked

FrontEnd platform based on AngularJS
FrontEnd platform based on AngularJSFrontEnd platform based on AngularJS
FrontEnd platform based on AngularJSEgor Miasnikov
 
Building Modern Web Apps with AngularJS
Building Modern Web Apps with AngularJSBuilding Modern Web Apps with AngularJS
Building Modern Web Apps with AngularJSRaveen Perera
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners WorkshopSathish VJ
 
Angular 1.5 Components
Angular 1.5 ComponentsAngular 1.5 Components
Angular 1.5 ComponentsJosé Barbosa
 
Scope demystified - AngularJS
Scope demystified - AngularJSScope demystified - AngularJS
Scope demystified - AngularJSSumanth 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 ComponentsRachael L Moore
 

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-manualLaura Popovici
 
Ujjwalreverseengineeringppptfinal
UjjwalreverseengineeringppptfinalUjjwalreverseengineeringppptfinal
Ujjwalreverseengineeringppptfinalujjwalchauhan87
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django frameworkKnoldus Inc.
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro anshu_atri
 
Corejavacoursesyllabus 140226051356-phpapp01
Corejavacoursesyllabus 140226051356-phpapp01Corejavacoursesyllabus 140226051356-phpapp01
Corejavacoursesyllabus 140226051356-phpapp01Sandeep Vishwakarma
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptxmrxyz19
 
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 newshyaminfo16
 
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 newdixonbakerr
 
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 newcharlesangles123
 
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 newchanduruc123
 
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 newshyaminfo40
 
Maven 2 features
Maven 2 featuresMaven 2 features
Maven 2 featuresAngel 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 newuopassignment
 
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 newlroselyn
 
Vikeshp
VikeshpVikeshp
VikeshpMdAsu1
 
Creating MVC Application with backbone js
Creating MVC Application with backbone jsCreating MVC Application with backbone js
Creating MVC Application with backbone jsMindfire Solutions
 

Similar to Dependency injection (20)

66781291 java-lab-manual
66781291 java-lab-manual66781291 java-lab-manual
66781291 java-lab-manual
 
Ujjwalreverseengineeringppptfinal
UjjwalreverseengineeringppptfinalUjjwalreverseengineeringppptfinal
Ujjwalreverseengineeringppptfinal
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
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
 

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

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Recently uploaded (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

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