SlideShare a Scribd company logo
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

More Related Content

What's hot

Maven
MavenMaven
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 javatwo2011
 
Spring jdbc dao
Spring jdbc daoSpring jdbc dao
Spring jdbc dao
Anuj Singh Rajput
 
Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVC
John Lewis
 
Spring by rj
Spring by rjSpring by rj
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
Lokesh Singrol
 
Spring core
Spring coreSpring core
Spring core
Harshit Choudhary
 
TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4
Lokesh Singrol
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
sourabh aggarwal
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
javatwo2011
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questionsDhiraj Champawat
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
BG Java EE Course
 
Spring
SpringSpring
Spring
gauravashq
 
Spring training
Spring trainingSpring training
Spring trainingshah_d_p
 

What's hot (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
 

Viewers also liked

Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
Sunil kumar Mohanty
 
Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injection
Steve Ng
 
Types of containers
Types of  containersTypes of  containers
Types of containers
Sagar Gadhiya(PaTel)
 
Types of containers
Types of containers Types of containers
Types of containers
MAX GALARZA HERNANDEZ
 
A Brief presentation on Containerisation
A Brief presentation on ContainerisationA Brief presentation on Containerisation
A Brief presentation on Containerisation
subhash_ae
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 

Viewers also liked (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
 

Similar to 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 JS
Ivano Malavolta
 
Dependency Injection in Spring
Dependency Injection in SpringDependency Injection in Spring
Dependency Injection in Spring
ASG
 
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 Profit
Mike Pfaff
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scala
Michal Bigos
 
Java beans
Java beansJava beans
Java beans
Sher Singh Bardhan
 
C questions
C questionsC questions
C questions
parm112
 
Struts 2
Struts 2Struts 2
Struts 2
Lalit Garg
 
Angular Framework ppt for beginners and advanced
Angular Framework ppt for beginners and advancedAngular Framework ppt for beginners and advanced
Angular Framework ppt for beginners and advanced
Preetha Ganapathi
 
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 4
than 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 Annotations
Anuj Singh Rajput
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Rajind Ruparathna
 
Bt0083 server side programing 2
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2
Techglyphs
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
Vinay Kumar
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
Hitesh 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
 
Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
Anuj Singh Rajput
 

Similar to 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
 
Angular Framework ppt for beginners and advanced
Angular Framework ppt for beginners and advancedAngular Framework ppt for beginners and advanced
Angular Framework ppt for beginners and advanced
 
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
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 

Recently uploaded (20)

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 

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