SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean ArchitectureRoc Boronat
 
Modern UI Development With Node.js
Modern UI Development With Node.jsModern UI Development With Node.js
Modern UI Development With Node.jsRyan Anklam
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the DomainVictor Rentea
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkHùng Nguyễn Huy
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in JavaIonut Bilica
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot IntroductionJeevesh Pandey
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Steve Pember
 
Introduction to Spring's Dependency Injection
Introduction to Spring's Dependency InjectionIntroduction to Spring's Dependency Injection
Introduction to Spring's Dependency InjectionRichard Paul
 
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!
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Edureka!
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOPDzmitry Naskou
 

Mais procurados (20)

Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
Modern UI Development With Node.js
Modern UI Development With Node.jsModern UI Development With Node.js
Modern UI Development With Node.js
 
Dependency injection ppt
Dependency injection pptDependency injection ppt
Dependency injection ppt
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the Domain
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
 
Introduction to Spring's Dependency Injection
Introduction to Spring's Dependency InjectionIntroduction to Spring's Dependency Injection
Introduction to Spring's Dependency Injection
 
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...
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring boot
Spring bootSpring boot
Spring boot
 
C# basics
 C# basics C# basics
C# basics
 

Destaque

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
 

Destaque (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
 

Semelhante a 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 newshyaminfo40
 
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
 
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 newlroselyn
 
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
 
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
 

Semelhante a 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
 

Mais de 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
 

Último

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
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?Igalia
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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.pdfsudhanshuwaghmare1
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 educationjfdjdjcjdnsjd
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
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?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

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