SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Different types of Dependency Injections and their Usages 
In this session I am going to focus on the various types of dependency 
injections in spring core. In the last session we had a brief overview on 
dependency injection and spring container . We have already discussed on the 
importance of dependency into object for various benefits. Now let us focus on 
the different types of dependency injection processes and there importance. The 
dependency injection option can be classified into two types. 
Dependency Injection Types: 
1. Constructor Injection 
2.SetterMethod Injection 
The constructor method of dependency injection is the process of injecting the 
dependencies of an object through it’s constructor arguments. In this mechanism 
the dependencies are pushed to the object through the constructor arguments at the 
time of instantiating it. The following code snippet shows the sample class that 
allows the injection of it’s dependencies as constructor arguments. 
StudentDAODBImpl.java 
package com.demo.spring; 
import java.sql.*; 
import javax.sql.*; 
public class StudentDAODBImpl implements StudentDAO { 
private DataSource dataSource; 
public StudentDAODBImpl(DataSource ds) { 
dataSource = ds; 
} 
public double getName(int rollNo) { 
//Use the datasource to handle the request 
} 
// Continue with the rest of the code 
}
In the above code snippet we can observe that StudentDAOImpl is dependent on 
the DataSource to complete the request that is to connect to the database it 
requires a javax.sql.DataSource object which is getting injected as a constructor 
argument here. Now I am going to explain how to configure the been definition 
describing the usages of constructor with arguments. 
Spring beans XML configuration tag to configure constructor arguments: 
The bean definition can described to use a constructor with zero or more 
arguments to instantiate the bean. the <constructor-arg> element describes one 
argument of the constructor , which means that to specify a constructor with more 
than one element we need to use <constructor-arg> element multiple times. The 
argument specified in the bean definition correspond to either a specific index of t 
he constructor argument list or supposed to be matched generally by types. 
The <constructor-arg> element supports four attributes as follows. 
1. index : This attribute takes the exact index in the constructor argument list. 
This can be used to avoid ambiguities like in the case of two arguments 
being of the same types. 
2. type : This attribute takes the type of this constructor argument. 
3. value : These are sub tag of the <constructor-arg> tag to represent values of the 
field. 
4. ref : This tag is used to refer to another bean to use it as a constructor 
argument. 
The <constructor-arg> element supports variety of types of as argument. Also the 
<constructor -arg> element supports various sub tags each of which allows us to 
describe different types of values. The various types of sub tags are as follows. 
1. Element : value 
Syntax : <value>content</value> 
Description : The value element describes the content is simple string 
representation which will be converted in to the argument type 
using the java bean PropertyEditors. 
Example : <constructor-arg type=“int”> 
<value>1000</value>
</constructor-arg> 
The “1000” content described by the <value> tag is converted into int type 
2. Element : null 
Syntax : <constructor-arg type=“String”> <null/></constructor-arg> 
or 
<constructor-arg type=“String”> 
<value><null/></value> 
</constructor-arg> 
Description : This is required to represent the null value, since the empty <value> 
element will be resolved into an empty string. 
3. Element : ref 
Syntax : <constructor-arg type=“String”> 
<ref local=“ds”/> 
</constructor-arg> 
Description : This tag references to another bean in this factory or an external 
factory. Here the external factory is the parent factory or the 
included factory. This sub tag supports three attributes: local, 
parent, bean 
local : It is used to refer to another bean with its Id in the same xml 
file. 
parent : It is used to refer to another bean with it’s id in the parent 
or included XML configuration file. 
bean : it is use to refer to another bean with it’s name in the same 
xml file. 
4. Element : list 
Description : it describes a java.util.List type. 
5. Element : set 
Description : it describes java.util.Set type 
6. Element : map
Syntax : 
<constructor-arg type=“String”> 
<map> 
<entry> 
<key> 
<value>name</value> 
</key> 
<value>sunil</value> 
</entry> 
<entry> 
<key> 
<value>address</value> 
</key> 
<value>TKL</value> 
</entry> 
</map> 
</constructor-arg> 
Description: It describes the java.util.Map type. A map can contains zero or 
more entry elements each of which describes one map entry. The 
map entry describes one key and value. 
7. Element : prop 
Syntax: 
<constructor-arg type=“java.util.Properties”> 
<props> 
<prop key=“name”>SNL</prop> 
<prop key=“address”>TKL</prop> 
</prop> 
</constructor-arg> 
Description : The <props> element describes a java.util.Properties type. It can 
contain zero or more <prop> elements where each <prop> 
element describes one entry. The property entry describes one ket 
and value. 
8. Element : bean 
Description : The bean element describes a bean 
In the above discussion we learnt about the <constructor-srg> elementa and it’s 
sub elements. The following code snippet shows the bean definition for the 
StudentDAODBImpl class in the spring beans XML configuration file. It 
instructs the spring IOC Core container to inject the DataStructure object into 
StudentDAODBImpl while creating an object. 
Code Snippet :
<beans> 
<bean id=“empdao” 
Class=“com.demo.spring.dao.StudentDAODBImpl”> 
<constructor-arg> 
<ref local=“ds”/> 
</constructor-arg> 
</bean> 
</beans> 
The above code snippet indicates the injection of bean with [id=“ds”] into 
StudentDAODBImpl object as an constructor argument . 
In the below code snippet I am going to discuss on Dependency injection during 
constructor overloading. 
Constructor OverLoading and Ambiguity resolution: 
In the below example there exists a class with two over loaded constructors. We 
are going to use the constructor injection in more efficient manner to over come 
the argument ambiguity. We will use the index or type attribute of the 
<constructor-arg> as per the necessity to resolute the conflict. 
Code Snippet: 
package com.demo.spring; 
public class DemoClass { 
public DemoClass(int id, String name) { 
// Some Code 
} 
public DemoClass(String name, int id) { 
// Some Code 
} 
} 
syntax 1: 
<!—bean definition in xml file > 
<!— Bean definition using the constructor int and string 
respectively > 
<bean id=“demo” class=“com.demo.spring.DemoClass”> 
<constructor-arg> 
<value>10</value> 
</constructor-arg>
<constructor-arg> 
<value>abc</value> 
</constructor-arg> 
</bean> 
syntax 2: 
<!—This bean definition uses constructor with argument string 
and int i.e here the the value 10 will be used as a string and 
the value 100 will be used as integer to initialize the 
constructor> 
<bean id=“demo” class=“com.demo.spring.DemoClass”> 
<constructor-arg type=“java.lang.String”> 
<value>10</value> 
</constructor-arg> 
<constructor-arg type=“java.lang.Integer”> 
<value>100</value> 
</constructor-arg> 
</bean> 
syntax 3: 
<!—This bean is using the constructor with argument int and 
String respectively. Here the constructor is getting recognized 
by considering the the index as well as the type of the 
constructor argument > 
<bean id=“demo” class=“com.demo.spring.DemoClass”> 
<constructor-arg index=“1” type=“java.lang.String”> 
<value>8082</value> 
</constructor-arg> 
<constructor-arg index=“0”> 
<value>82</value> 
</constructor-arg> 
</bean> 
The above syntaxes describes the various types of configuration formats with 
<constructor-arg> element to get the required constructor. The arguments starting 
index is always 0 and it is recommended to use the index if more number of 
arguments are going to be get configured . It is wise to use Constructor injection 
when there exists minimal number of dependencies. 
Setter Injection : 
Here dependencies of an objects are used to get injected using the setter method. It 
is efficient to use setter injection when there exists more number of dependencies. 
The following sample code will describe a demo class which allows its 
dependencies to be get injected using the setter method of injection.
import com.demo.spring.dao.StudentDAO; 
public class StudentServiceImpl implements StudentService { 
private StudentDAO studentDAO; 
public void setStudentDAO(StudentDAO studentDAO) { 
this.studentDAO = studentDAO; 
} 
// Some Code 
} 
In the above example we can see that the StudentServiceImpl is dependent on 
StudentDAO. The StudentDAO object is required to perform the persistence 
operation and it is getting injected using the setter method to the 
StudentServiceImpl class. The below discussion describes how to configure the 
bean definition and also describes how to use setter method injection. 
Use of property Element : 
The bean definition can use zero or more properties to inject before making the 
bean object available to the application. The property element is used to set the 
arguments for the setter method. The property element support three arguments as 
described below. 
name : This attribute takes the name of java bean property. It uses the Java bean 
naming convention like a name “dao” will correspond to setDao as a 
setter method. 
value : It corresponds to the child element “value” as described in the under the 
constructor injection. 
ref : it refers to a child element “ref bean=” 
The child elements for the <property> element are same as of the <constructor-arg> 
described in previously. 
example: 
<bean id=“demo” class=“com.demo.spring.StudentServiceImpl”> 
<property name=“studentDAO”> 
<ref local=“studentdao”> 
</property>
</bean> 
The above code indicates that to set a reference of a spring bean object named 
studentdao to the property ‘studentDAO’ of StudentServiceImpl . 
Importance of using the property namespace: 
Spring 2.0 provides the support to use our own tag with our own name name 
space as per our requirement an d as per the convenience. This property name 
space provides a way to simplify the configuration document. It eliminates the use 
of <property> tag for specifying the the property injection. 
For the use of property namespace we need to import the following name space 
xmlns:p=“http://www.springframework.org/schema/p” 
Importing this name space in bean tag allows us to use the property namespace 
with prefix ‘p’ throughout the definition. We use the property name as an attribute 
name with prefix ‘p:’ to specify the value for the property. 
<bean id: ” demoBean ” class=“com.demo.spring.DemoBean” 
p:message=“HelloWorld” /> 
This specifies to set a spring value HelloWorld to the property ‘message’ of 
DemoBean. If it is required to refer to another bean as a value for the property, we 
need to append ‘-ref’ to the property name. The below code snippet demonstrates 
it. 
<bean id=“demoBean1” class=“com.demo.spring.DemoBean1”/> 
<bean id=“demoBean2” class=“com.spring.DemoBean2” 
p:demoBean-ref=“demoBean1/”> 
This code snippet specifies to set reference of a spring bean object . named 
“demoBean1” to the property ‘myBean’ of myBean2 . It invokes the 
setDemoBean(DemoBean1 arg). 
Dependency Injection Example : 
In the preceding discussion we have brief idea on the dependency injection. Now 
we are going to design an example to demonstrate the constructor and setter 
method injection. 
StudentServices.java shows a simple business object interface that represents the
services related to student. 
package com.demo.spring.service; 
/** 
* interface for the business object 
* 
* @author sunilm 
* 
*/ 
public interface StudentService { 
public boolean insertStudentInfo(int id, String name); 
} 
StudentServiceImp.java is the implementation of the StudentService.java 
StudentServiceImp.java 
package com.demo.spring.service; 
import com.demo.spring.dao.StudentDAO; 
/** 
* Implementation of the StudentService Interface 
* 
* @author sunilm 
* 
*/ 
public class StudentServiceImpl implements StudentService{ 
private StudentDAO studentDAO ; 
/** 
* set StudentDAO 
* 
* @param studentDAO 
*/ 
public void setStudentDAO(StudentDAO studentDAO) { 
this.studentDAO = studentDAO; 
}
/** 
* insert the student name to the database and check the correctness 
by retrieving it 
* corresponding to the same student id 
* @param id student id 
* @param name name of the student 
* @return insertationStatus status of execution 
*/ 
public boolean insertStudentInfo(int id, String name) { 
boolean insertationStatus = false; 
studentDAO.setName(id, name); 
String studentName = studentDAO.getName(id); 
if(name.equals(studentName)) { 
insertationStatus = true; 
} 
return insertationStatus; 
} 
/** 
* Extracts the name of the student 
* @param id students unique id 
* @return name of the student 
*/ 
public String extractStudentInfo(int id) { 
return studentDAO.getName(id); 
} 
} 
StudentDAO.java interface with a minimum set of methods , which will be 
enhanced further as per the requirement. 
package com.demo.spring.dao; 
/** 
* Interface for the business Object 
*
* @author sunilm 
* 
*/ 
public interface StudentDAO { 
String getName(int id); 
void setName(int id, String name); 
} 
StudentDAODBImpl.java is the enhancement of the StudentDAO.java interface 
StudentDAODBImpl.java 
package com.demo.spring.dao; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import javax.sql.DataSource; 
/** 
* Implementation of the Studentservice[BusinessObjectinterface] 
* @author sunilm 
* 
*/ 
public class StudentDAODBImpl implements StudentDAO { 
private DataSource dataSource; 
/** 
* Constructor to initialize the datasource 
* 
* @param dataSource 
*/ 
public StudentDAODBImpl(DataSource dataSource) { 
this.dataSource=dataSource; 
}
/** 
* Retrieve the name of the student with respect to the provided id from 
the database 
* @param id id of the student 
* @return name name of the student 
* 
*/ 
public String getName(int id) { 
Connection con = null; 
String studentName = ""; 
try { 
con = dataSource.getConnection(); 
PreparedStatement ps = con.prepareStatement("select name 
from student where id=?"); 
ps.setInt(1, id); 
ResultSet rs=ps.executeQuery(); 
if (rs.next()) { 
studentName = rs.getString(1); 
} else { 
throw new RuntimeException("Student not found"); 
} 
} catch (SQLException e) { 
e.printStackTrace(); 
} finally { 
try { 
con.close(); 
} catch (SQLException e) { 
// log the error 
e.printStackTrace(); 
} 
} return studentName; 
} 
/** 
* Keep the information of the student in the database 
* @param id unique id for the student 
* @param name name of the student
*/ 
public void setName(int id, String name) { 
Connection con = null; 
try { 
con = dataSource.getConnection(); 
PreparedStatement ps = con.prepareStatement("update student 
set name=? where id=?"); 
ps.setString(1, "piixbuf"); 
ps.setInt(2, 101); 
int count = ps.executeUpdate(); 
if(count == 1 || count == Statement.SUCCESS_NO_INFO) { 
return; 
} 
} catch (SQLException e) { 
e.printStackTrace(); 
} finally { 
try{ 
con.close(); 
} catch(SQLException e){ 
e.printStackTrace(); 
} 
} 
} 
} 
Now we have to configure these beans into spring beans XML configuration file. 
The following demobeans.xml shows the spring bean XML configuration file 
with respective datasource . 
Demobeans.xml 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id = "studentDAO" class = "com.spring.demo.dao.StudentDAODBImpl" >
<constructor-arg> 
<ref bean="ds"/> 
</constructor-arg> 
</bean> 
<bean id = "ds" class= "org.apache.commons.dbcp.BasicDataSource"> 
<property name="driverClassName"> 
<value>oracle.jdbc.driver.OracleDriver</value> 
</property> 
<property name="url"> 
<value>jdbc:oracle:thin:@localhost:1521:orcl</value> 
</property> 
<property name="username"> 
<value>scott</value> 
</property> 
<property name="password"> 
<value>tiger</value> 
</property> 
</bean> 
<bean id="studentService" class="com.demo.spring.StudentServiceImpl"> 
<property name="studentDAO"> 
<ref bean="studentDAO"/> 
</property> 
</bean> 
</beans> 
In the above example we have completed the business implementation and spring 
XML configuration. Now we have to write the test class to test the StudentService 
class. It will insert a students name into the data base against a specific ID. After 
that in the next step it will retrieve the same student name using that specific ID. 
Now it compares the inserted data with the retrieved the value to check if the 
execution took place successfully or not. In the above example both setter and 
constructor injection got used. 
StudentServiceTest.java 
package com.demo.spring.test; 
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.FileSystemResource; 
import com.demo.spring.service.StudentService; 
/**
* Test the StudentService class execution 
* @author sunilm 
* 
*/ 
public class StudentServiceTest { 
public static void main(String[] args) { 
String studentName = "pixbuf"; 
BeanFactory beans = new XmlBeanFactory(new 
FileSystemResource("demobeans.xml")); 
StudentService stdService = 
(StudentService)beans.getBean("studentService"); 
stdService.insertStudentInfo(101, studentName); 
String nameFromDataBase = 
stdService.extractStudentInfo(101); 
if (studentName.equalsIgnoreCase(nameFromDataBase)) { 
System.out.println("Excution Success"); 
} 
} 
} 
Hope this discussion helps in understanding the different 
types of dependency injections as well as there uses. Thank you all

Weitere ähnliche Inhalte

Was ist angesagt? (19)

JEE5 New Features
JEE5 New FeaturesJEE5 New Features
JEE5 New Features
 
Maven
MavenMaven
Maven
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望
 
Spring jdbc dao
Spring jdbc daoSpring jdbc dao
Spring jdbc dao
 
Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVC
 
Working with Servlets
Working with ServletsWorking with Servlets
Working with Servlets
 
Spring by rj
Spring by rjSpring by rj
Spring by rj
 
Deployment
DeploymentDeployment
Deployment
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
 
Spring core
Spring coreSpring core
Spring core
 
TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4
 
Spring annotation
Spring annotationSpring annotation
Spring annotation
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
 
Spring
SpringSpring
Spring
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Spring training
Spring trainingSpring training
Spring training
 

Andere mochten auch

Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injectionSteve Ng
 
A Brief presentation on Containerisation
A Brief presentation on ContainerisationA Brief presentation on Containerisation
A Brief presentation on Containerisationsubhash_ae
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 

Andere mochten auch (6)

Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injection
 
Types of containers
Types of  containersTypes of  containers
Types of containers
 
Types of containers
Types of containers Types of containers
Types of containers
 
A Brief presentation on Containerisation
A Brief presentation on ContainerisationA Brief presentation on Containerisation
A Brief presentation on Containerisation
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 

Ähnlich wie Types of Dependency Injection in Spring

[2015/2016] Require JS and Handlebars JS
[2015/2016] Require JS and Handlebars JS[2015/2016] Require JS and Handlebars JS
[2015/2016] Require JS and Handlebars JSIvano Malavolta
 
Dependency Injection in Spring
Dependency Injection in SpringDependency Injection in Spring
Dependency Injection in SpringASG
 
Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language Mahika Tutorials
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitMike Pfaff
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scalaMichal Bigos
 
C questions
C questionsC questions
C questionsparm112
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4than sare
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directivesAlexe Bogdan
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and AnnotationsAnuj Singh Rajput
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkRajind Ruparathna
 
Bt0083 server side programing 2
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2Techglyphs
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depthVinay Kumar
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beansHitesh Parmar
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part OneAppcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part OneAaron Saunders
 

Ähnlich wie Types of Dependency Injection in Spring (20)

[2015/2016] Require JS and Handlebars JS
[2015/2016] Require JS and Handlebars JS[2015/2016] Require JS and Handlebars JS
[2015/2016] Require JS and Handlebars JS
 
Dependency Injection in Spring
Dependency Injection in SpringDependency Injection in Spring
Dependency Injection in Spring
 
Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scala
 
Java beans
Java beansJava beans
Java beans
 
C questions
C questionsC questions
C questions
 
Struts 2
Struts 2Struts 2
Struts 2
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Bt0083 server side programing 2
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
 
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part OneAppcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
 

Kürzlich hochgeladen

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
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
 
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
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
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
 
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
 
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
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
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
 

Kürzlich hochgeladen (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
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
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
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?
 
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
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
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
 
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
 
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
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
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
 

Types of Dependency Injection in Spring

  • 1. Different types of Dependency Injections and their Usages In this session I am going to focus on the various types of dependency injections in spring core. In the last session we had a brief overview on dependency injection and spring container . We have already discussed on the importance of dependency into object for various benefits. Now let us focus on the different types of dependency injection processes and there importance. The dependency injection option can be classified into two types. Dependency Injection Types: 1. Constructor Injection 2.SetterMethod Injection The constructor method of dependency injection is the process of injecting the dependencies of an object through it’s constructor arguments. In this mechanism the dependencies are pushed to the object through the constructor arguments at the time of instantiating it. The following code snippet shows the sample class that allows the injection of it’s dependencies as constructor arguments. StudentDAODBImpl.java package com.demo.spring; import java.sql.*; import javax.sql.*; public class StudentDAODBImpl implements StudentDAO { private DataSource dataSource; public StudentDAODBImpl(DataSource ds) { dataSource = ds; } public double getName(int rollNo) { //Use the datasource to handle the request } // Continue with the rest of the code }
  • 2. In the above code snippet we can observe that StudentDAOImpl is dependent on the DataSource to complete the request that is to connect to the database it requires a javax.sql.DataSource object which is getting injected as a constructor argument here. Now I am going to explain how to configure the been definition describing the usages of constructor with arguments. Spring beans XML configuration tag to configure constructor arguments: The bean definition can described to use a constructor with zero or more arguments to instantiate the bean. the <constructor-arg> element describes one argument of the constructor , which means that to specify a constructor with more than one element we need to use <constructor-arg> element multiple times. The argument specified in the bean definition correspond to either a specific index of t he constructor argument list or supposed to be matched generally by types. The <constructor-arg> element supports four attributes as follows. 1. index : This attribute takes the exact index in the constructor argument list. This can be used to avoid ambiguities like in the case of two arguments being of the same types. 2. type : This attribute takes the type of this constructor argument. 3. value : These are sub tag of the <constructor-arg> tag to represent values of the field. 4. ref : This tag is used to refer to another bean to use it as a constructor argument. The <constructor-arg> element supports variety of types of as argument. Also the <constructor -arg> element supports various sub tags each of which allows us to describe different types of values. The various types of sub tags are as follows. 1. Element : value Syntax : <value>content</value> Description : The value element describes the content is simple string representation which will be converted in to the argument type using the java bean PropertyEditors. Example : <constructor-arg type=“int”> <value>1000</value>
  • 3. </constructor-arg> The “1000” content described by the <value> tag is converted into int type 2. Element : null Syntax : <constructor-arg type=“String”> <null/></constructor-arg> or <constructor-arg type=“String”> <value><null/></value> </constructor-arg> Description : This is required to represent the null value, since the empty <value> element will be resolved into an empty string. 3. Element : ref Syntax : <constructor-arg type=“String”> <ref local=“ds”/> </constructor-arg> Description : This tag references to another bean in this factory or an external factory. Here the external factory is the parent factory or the included factory. This sub tag supports three attributes: local, parent, bean local : It is used to refer to another bean with its Id in the same xml file. parent : It is used to refer to another bean with it’s id in the parent or included XML configuration file. bean : it is use to refer to another bean with it’s name in the same xml file. 4. Element : list Description : it describes a java.util.List type. 5. Element : set Description : it describes java.util.Set type 6. Element : map
  • 4. Syntax : <constructor-arg type=“String”> <map> <entry> <key> <value>name</value> </key> <value>sunil</value> </entry> <entry> <key> <value>address</value> </key> <value>TKL</value> </entry> </map> </constructor-arg> Description: It describes the java.util.Map type. A map can contains zero or more entry elements each of which describes one map entry. The map entry describes one key and value. 7. Element : prop Syntax: <constructor-arg type=“java.util.Properties”> <props> <prop key=“name”>SNL</prop> <prop key=“address”>TKL</prop> </prop> </constructor-arg> Description : The <props> element describes a java.util.Properties type. It can contain zero or more <prop> elements where each <prop> element describes one entry. The property entry describes one ket and value. 8. Element : bean Description : The bean element describes a bean In the above discussion we learnt about the <constructor-srg> elementa and it’s sub elements. The following code snippet shows the bean definition for the StudentDAODBImpl class in the spring beans XML configuration file. It instructs the spring IOC Core container to inject the DataStructure object into StudentDAODBImpl while creating an object. Code Snippet :
  • 5. <beans> <bean id=“empdao” Class=“com.demo.spring.dao.StudentDAODBImpl”> <constructor-arg> <ref local=“ds”/> </constructor-arg> </bean> </beans> The above code snippet indicates the injection of bean with [id=“ds”] into StudentDAODBImpl object as an constructor argument . In the below code snippet I am going to discuss on Dependency injection during constructor overloading. Constructor OverLoading and Ambiguity resolution: In the below example there exists a class with two over loaded constructors. We are going to use the constructor injection in more efficient manner to over come the argument ambiguity. We will use the index or type attribute of the <constructor-arg> as per the necessity to resolute the conflict. Code Snippet: package com.demo.spring; public class DemoClass { public DemoClass(int id, String name) { // Some Code } public DemoClass(String name, int id) { // Some Code } } syntax 1: <!—bean definition in xml file > <!— Bean definition using the constructor int and string respectively > <bean id=“demo” class=“com.demo.spring.DemoClass”> <constructor-arg> <value>10</value> </constructor-arg>
  • 6. <constructor-arg> <value>abc</value> </constructor-arg> </bean> syntax 2: <!—This bean definition uses constructor with argument string and int i.e here the the value 10 will be used as a string and the value 100 will be used as integer to initialize the constructor> <bean id=“demo” class=“com.demo.spring.DemoClass”> <constructor-arg type=“java.lang.String”> <value>10</value> </constructor-arg> <constructor-arg type=“java.lang.Integer”> <value>100</value> </constructor-arg> </bean> syntax 3: <!—This bean is using the constructor with argument int and String respectively. Here the constructor is getting recognized by considering the the index as well as the type of the constructor argument > <bean id=“demo” class=“com.demo.spring.DemoClass”> <constructor-arg index=“1” type=“java.lang.String”> <value>8082</value> </constructor-arg> <constructor-arg index=“0”> <value>82</value> </constructor-arg> </bean> The above syntaxes describes the various types of configuration formats with <constructor-arg> element to get the required constructor. The arguments starting index is always 0 and it is recommended to use the index if more number of arguments are going to be get configured . It is wise to use Constructor injection when there exists minimal number of dependencies. Setter Injection : Here dependencies of an objects are used to get injected using the setter method. It is efficient to use setter injection when there exists more number of dependencies. The following sample code will describe a demo class which allows its dependencies to be get injected using the setter method of injection.
  • 7. import com.demo.spring.dao.StudentDAO; public class StudentServiceImpl implements StudentService { private StudentDAO studentDAO; public void setStudentDAO(StudentDAO studentDAO) { this.studentDAO = studentDAO; } // Some Code } In the above example we can see that the StudentServiceImpl is dependent on StudentDAO. The StudentDAO object is required to perform the persistence operation and it is getting injected using the setter method to the StudentServiceImpl class. The below discussion describes how to configure the bean definition and also describes how to use setter method injection. Use of property Element : The bean definition can use zero or more properties to inject before making the bean object available to the application. The property element is used to set the arguments for the setter method. The property element support three arguments as described below. name : This attribute takes the name of java bean property. It uses the Java bean naming convention like a name “dao” will correspond to setDao as a setter method. value : It corresponds to the child element “value” as described in the under the constructor injection. ref : it refers to a child element “ref bean=” The child elements for the <property> element are same as of the <constructor-arg> described in previously. example: <bean id=“demo” class=“com.demo.spring.StudentServiceImpl”> <property name=“studentDAO”> <ref local=“studentdao”> </property>
  • 8. </bean> The above code indicates that to set a reference of a spring bean object named studentdao to the property ‘studentDAO’ of StudentServiceImpl . Importance of using the property namespace: Spring 2.0 provides the support to use our own tag with our own name name space as per our requirement an d as per the convenience. This property name space provides a way to simplify the configuration document. It eliminates the use of <property> tag for specifying the the property injection. For the use of property namespace we need to import the following name space xmlns:p=“http://www.springframework.org/schema/p” Importing this name space in bean tag allows us to use the property namespace with prefix ‘p’ throughout the definition. We use the property name as an attribute name with prefix ‘p:’ to specify the value for the property. <bean id: ” demoBean ” class=“com.demo.spring.DemoBean” p:message=“HelloWorld” /> This specifies to set a spring value HelloWorld to the property ‘message’ of DemoBean. If it is required to refer to another bean as a value for the property, we need to append ‘-ref’ to the property name. The below code snippet demonstrates it. <bean id=“demoBean1” class=“com.demo.spring.DemoBean1”/> <bean id=“demoBean2” class=“com.spring.DemoBean2” p:demoBean-ref=“demoBean1/”> This code snippet specifies to set reference of a spring bean object . named “demoBean1” to the property ‘myBean’ of myBean2 . It invokes the setDemoBean(DemoBean1 arg). Dependency Injection Example : In the preceding discussion we have brief idea on the dependency injection. Now we are going to design an example to demonstrate the constructor and setter method injection. StudentServices.java shows a simple business object interface that represents the
  • 9. services related to student. package com.demo.spring.service; /** * interface for the business object * * @author sunilm * */ public interface StudentService { public boolean insertStudentInfo(int id, String name); } StudentServiceImp.java is the implementation of the StudentService.java StudentServiceImp.java package com.demo.spring.service; import com.demo.spring.dao.StudentDAO; /** * Implementation of the StudentService Interface * * @author sunilm * */ public class StudentServiceImpl implements StudentService{ private StudentDAO studentDAO ; /** * set StudentDAO * * @param studentDAO */ public void setStudentDAO(StudentDAO studentDAO) { this.studentDAO = studentDAO; }
  • 10. /** * insert the student name to the database and check the correctness by retrieving it * corresponding to the same student id * @param id student id * @param name name of the student * @return insertationStatus status of execution */ public boolean insertStudentInfo(int id, String name) { boolean insertationStatus = false; studentDAO.setName(id, name); String studentName = studentDAO.getName(id); if(name.equals(studentName)) { insertationStatus = true; } return insertationStatus; } /** * Extracts the name of the student * @param id students unique id * @return name of the student */ public String extractStudentInfo(int id) { return studentDAO.getName(id); } } StudentDAO.java interface with a minimum set of methods , which will be enhanced further as per the requirement. package com.demo.spring.dao; /** * Interface for the business Object *
  • 11. * @author sunilm * */ public interface StudentDAO { String getName(int id); void setName(int id, String name); } StudentDAODBImpl.java is the enhancement of the StudentDAO.java interface StudentDAODBImpl.java package com.demo.spring.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; /** * Implementation of the Studentservice[BusinessObjectinterface] * @author sunilm * */ public class StudentDAODBImpl implements StudentDAO { private DataSource dataSource; /** * Constructor to initialize the datasource * * @param dataSource */ public StudentDAODBImpl(DataSource dataSource) { this.dataSource=dataSource; }
  • 12. /** * Retrieve the name of the student with respect to the provided id from the database * @param id id of the student * @return name name of the student * */ public String getName(int id) { Connection con = null; String studentName = ""; try { con = dataSource.getConnection(); PreparedStatement ps = con.prepareStatement("select name from student where id=?"); ps.setInt(1, id); ResultSet rs=ps.executeQuery(); if (rs.next()) { studentName = rs.getString(1); } else { throw new RuntimeException("Student not found"); } } catch (SQLException e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { // log the error e.printStackTrace(); } } return studentName; } /** * Keep the information of the student in the database * @param id unique id for the student * @param name name of the student
  • 13. */ public void setName(int id, String name) { Connection con = null; try { con = dataSource.getConnection(); PreparedStatement ps = con.prepareStatement("update student set name=? where id=?"); ps.setString(1, "piixbuf"); ps.setInt(2, 101); int count = ps.executeUpdate(); if(count == 1 || count == Statement.SUCCESS_NO_INFO) { return; } } catch (SQLException e) { e.printStackTrace(); } finally { try{ con.close(); } catch(SQLException e){ e.printStackTrace(); } } } } Now we have to configure these beans into spring beans XML configuration file. The following demobeans.xml shows the spring bean XML configuration file with respective datasource . Demobeans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "studentDAO" class = "com.spring.demo.dao.StudentDAODBImpl" >
  • 14. <constructor-arg> <ref bean="ds"/> </constructor-arg> </bean> <bean id = "ds" class= "org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="username"> <value>scott</value> </property> <property name="password"> <value>tiger</value> </property> </bean> <bean id="studentService" class="com.demo.spring.StudentServiceImpl"> <property name="studentDAO"> <ref bean="studentDAO"/> </property> </bean> </beans> In the above example we have completed the business implementation and spring XML configuration. Now we have to write the test class to test the StudentService class. It will insert a students name into the data base against a specific ID. After that in the next step it will retrieve the same student name using that specific ID. Now it compares the inserted data with the retrieved the value to check if the execution took place successfully or not. In the above example both setter and constructor injection got used. StudentServiceTest.java package com.demo.spring.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; import com.demo.spring.service.StudentService; /**
  • 15. * Test the StudentService class execution * @author sunilm * */ public class StudentServiceTest { public static void main(String[] args) { String studentName = "pixbuf"; BeanFactory beans = new XmlBeanFactory(new FileSystemResource("demobeans.xml")); StudentService stdService = (StudentService)beans.getBean("studentService"); stdService.insertStudentInfo(101, studentName); String nameFromDataBase = stdService.extractStudentInfo(101); if (studentName.equalsIgnoreCase(nameFromDataBase)) { System.out.println("Excution Success"); } } } Hope this discussion helps in understanding the different types of dependency injections as well as there uses. Thank you all