In this session I have described the different types of dependency injections and their usages.
Reference Link : http://en.wikipedia.org/wiki/Dependency_injection
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