Dependency
injection by Constructor in Spring Container:
- We can inject the dependency by constructor in spring framework, use the <constructor-arg> sub tag of <beans> tag in the bean configuration file and supply the values.
- If the constructor parameter type is simple type, to supply value we can use “value” attribute.
- If the constructor parameter type is Object type, to supply value we can use “ref” attribute.
- Let’s see the sample example to inject sample (primitive and standard based values) and reference (object) type values. Follow the below steps for Constructor dependency injection (DI).
Step-1: Project
Structure.
Step-2: Create
Spring Bean class with the required dependencies.
Step-3: Define
the parameterized constructor to support for constructor DI.
Step-4: Use the
<constructor-arg> tag in bean configuration file.
Step-5: Get the
bean object from beans configuration file in Client app.
Step-6: Required
jar/dependency configuration in pom.xml file.
Step-7: Output of
Application
Step-1:
Project Structure.
Creating a Department.java class with required fields, in
this example I have created with 3 fields, ie., deptNo, deptName and deptDesc,
all are String data type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gurugubellitech.bean; | |
public class Department { | |
private String deptNo; | |
private String deptName; | |
private String deptDesc; | |
/** | |
* @return the deptNo | |
*/ | |
public String getDeptNo() { | |
return deptNo; | |
} | |
/** | |
* @param deptNo | |
* the deptNo to set | |
*/ | |
public void setDeptNo(String deptNo) { | |
this.deptNo = deptNo; | |
} | |
/** | |
* @return the deptName | |
*/ | |
public String getDeptName() { | |
return deptName; | |
} | |
/** | |
* @param deptName | |
* the deptName to set | |
*/ | |
public void setDeptName(String deptName) { | |
this.deptName = deptName; | |
} | |
/** | |
* @return the deptDesc | |
*/ | |
public String getDeptDesc() { | |
return deptDesc; | |
} | |
/** | |
* @param deptDesc | |
* the deptDesc to set | |
*/ | |
public void setDeptDesc(String deptDesc) { | |
this.deptDesc = deptDesc; | |
} | |
} |
Step-3:
Define the parameterized constructor to support for constructor DI:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gurugubellitech.bean; | |
public class Employee { | |
private String empNo; | |
private String firstName; | |
private String lastName; | |
private Department department; | |
/** | |
* @return the empNo | |
*/ | |
public String getEmpNo() { | |
return empNo; | |
} | |
/** | |
* @param empNo | |
* the empNo to set | |
*/ | |
public void setEmpNo(String empNo) { | |
this.empNo = empNo; | |
} | |
/** | |
* @return the firstName | |
*/ | |
public String getFirstName() { | |
return firstName; | |
} | |
/** | |
* @param firstName | |
* the firstName to set | |
*/ | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
/** | |
* @return the lastName | |
*/ | |
public String getLastName() { | |
return lastName; | |
} | |
/** | |
* @param lastName | |
* the lastName to set | |
*/ | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public Department getDepartment() { | |
return department; | |
} | |
public void setDepartment(Department department) { | |
this.department = department; | |
} | |
/** | |
* @param empNo | |
* @param firstName | |
* @param lastName | |
*/ | |
public Employee(String empNo, String firstName, String lastName) { | |
this.empNo = empNo; | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
/** | |
* @param empNo | |
* @param firstName | |
* @param lastName | |
* @param department | |
*/ | |
public Employee(String empNo, String firstName, String lastName, | |
Department department) { | |
this.empNo = empNo; | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.department = department; | |
} | |
} |
Step-4: Use
the <constructor-arg> tag in bean configuration file:
Below is the beans configuration file, provided by spring
framework, we need to configure all the bean object information in this file as
per the spring API. In the below file we configured Employee and Department
object information, and getting this object in TestApp.java class by using bean
class name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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-3.0.xsd"> | |
<!-- Data about Employee1 --> | |
<bean id="employee1" class="com.gurugubellitech.bean.Employee"> | |
<constructor-arg name="empNo" value="EMP1001"/> | |
<constructor-arg name="firstName" value="Gurugubelli"/> | |
<constructor-arg name="lastName" value="Technologies"/> | |
</bean> | |
<bean id="employee2" class="com.gurugubellitech.bean.Employee"> | |
<constructor-arg name="empNo" value="EMP1002"/> | |
<constructor-arg name="firstName" value="Gurugubelli"/> | |
<constructor-arg name="lastName" value="Technologies"/> | |
<constructor-arg name="department" ref="dept2"/> | |
</bean> | |
<bean id="dept2" class="com.gurugubellitech.bean.Department"> | |
<property name="deptNo" value="10"></property> | |
<property name="deptName" value="CS and IS"></property> | |
<property name="deptDesc" value="Department of Computer science And information systems"></property> | |
</bean> | |
<!-- Data about Employee2 --> | |
<bean id="employee3" class="com.gurugubellitech.bean.Employee"> | |
<constructor-arg name="empNo" value="EMP1003"/> | |
<constructor-arg name="firstName" value="Gurugubelli"/> | |
<constructor-arg name="lastName" value="Tech"/> | |
<constructor-arg name="department" ref="dept3"/> | |
</bean> | |
<bean id="dept3" class="com.gurugubellitech.bean.Department"> | |
<property name="deptNo" value="20"></property> | |
<property name="deptName" value="M and S"></property> | |
<property name="deptDesc" value="Department of Mathematics And Statistics"></property> | |
</bean> | |
</beans> |
Step-5: Get
the bean object from beans configuration file in Client app:
TestApp.java class is a main method class and getting bean
objects using bean name in my-beans.xml file configuration with the help of ApplicationContext
object provided by spring framework.
In this example we are getting two types of objects from
beans configuration file. One object is Employee object and another object also
Employee object with Department object. (Department object is available in
Employee object as per the Employee.java class declaration.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gurugubellitech.client; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import com.gurugubellitech.bean.Department; | |
import com.gurugubellitech.bean.Employee; | |
public class TestApp { | |
private static ApplicationContext context; | |
public static void main(String[] args) { | |
String configFile = "my-beans.xml"; | |
context = new ClassPathXmlApplicationContext(configFile); | |
Employee emp1 = context.getBean("employee1", Employee.class); | |
System.out.println("\n======= Employee Details with out Address ======="); | |
System.out.println("Employee ID : " + emp1.getEmpNo()); | |
System.out.println("First Name : " + emp1.getFirstName()); | |
System.out.println("Last Name : " + emp1.getLastName()); | |
Employee emp2 = context.getBean("employee2", Employee.class); | |
System.out.println("\n======= Employee2 Details with Dept info ======="); | |
System.out.println("Employee ID : " + emp2.getEmpNo()); | |
System.out.println("First Name : " + emp2.getFirstName()); | |
System.out.println("Last Name : " + emp2.getLastName()); | |
Department dept2 = emp2.getDepartment(); | |
System.out.println("Dept NO : " + dept2.getDeptNo()); | |
System.out.println("Dept Name : " + dept2.getDeptName()); | |
System.out.println("Dept Desc : " + dept2.getDeptDesc()); | |
Employee emp3 = context.getBean("employee3", Employee.class); | |
System.out.println("\n======= Employee3 Details with Dept info ======="); | |
System.out.println("Employee ID : " + emp3.getEmpNo()); | |
System.out.println("First Name : " + emp3.getFirstName()); | |
System.out.println("Last Name : " + emp3.getLastName()); | |
Department dept3 = emp3.getDepartment(); | |
System.out.println("Dept NO : " + dept3.getDeptNo()); | |
System.out.println("Dept Name : " + dept3.getDeptName()); | |
System.out.println("Dept Desc : " + dept3.getDeptDesc()); | |
} | |
} |
Step-6:
Required jar/dependency configuration in pom.xml file:
Configured required spring dependencies/jar information in
pom.xml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.gurugubellitech</groupId> | |
<artifactId>maven-spring-constructor-injection</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-beans</artifactId> | |
<version>5.0.2.RELEASE</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
<version>5.0.2.RELEASE</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>5.0.2.RELEASE</version> | |
</dependency> | |
</dependencies> | |
</project> |
Step-7:
Output of Application:
Dependency injection by Constructor in Spring Container:
Reviewed by Gurugubelli Technologies
on
December 31, 2017
Rating:

No comments: