Spring IoC ? Dependencies injection Examples
Welcome to second part of Spring Dependency Injection tutorial series, in previous article we came across a quick introduction about Spring IOC and Spring DI. In this particular blog we will see Spring IoC via Constructor method in details.Constructor Injection Examples in Spring
Constructor injection is second approach to implement Depenendency injection in Spring. Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.Situation
The scenario is that we have two classes, StudentImpl and StudentPhoneNumberImpl , each class is implementing an interface Student and StudentPhoneNumber respectively.Student.java ? Interface
A simple java interface having a single method declaration in it.package com.beingjavaguys.domain; public interface Student { public String getStudentDetails(); }
StudentImpl ? Class
Simple java class implementing Student interface and override getStudentDetails() method.package com.beingjavaguys.domain; public class StudentImpl implements Student { @Override public String getStudentDetails() { // TODO Auto-generated method stub return null; } }
StudentPhoneNumber.java ? Interface
Simple interface with a single method declaration in it. This is the method that we will try to call in StudentImpl class.package com.beingjavaguys.domain; public interface StudentPhoneNumber { public String getStudentPhone(); }
StudentPhoneNumberImpl.java ? Class
StudentPhoneNumberImpl class is a simple class, to call any of its method we need to create an hard coded object of this class. But in this blog we will call it's methods using a reference of StudentPhoneNumber interface.package com.beingjavaguys.domain; public class StudentPhoneNumberImp implements StudentPhoneNumber { @Override public String getStudentPhone() { // TODO Auto-generated method stub return "8767567546"; } }
Objective
Our objective is to call ?getStudentPhone()? method of StudentPhoneNumberImpl class in StudentImpl class without creating a dependency in between these two classes. This can be done using constructor injection method of spring ioc.Solution
We need to do two things here :1) Register the two classes as beans in servlet-context and define their relation. See the configuration:
<bean id="studentImpl" class="com.beingjavaguys.domain.StudentImpl"> <constructor-arg ref="studentPhoneNumberImp" /> </bean> <bean id="studentPhoneNumberImp" class="com.beingjavaguys.domain.StudentPhoneNumberImp" />First of all we have defined two classes here as a bean, and than we have injected a property named studentPhone as a property of StudentImpl class. Now this property studentPhone will be used as an object of ?StudentPhoneImpl? class inside StudentImpl Class.
2) Define ?studentPhone? property in StudentImpl class and make an pass an object of StudentPhoneNumber in constructor of StudentImpl.
package com.beingjavaguys.domain; public class StudentImpl implements Student { private StudentPhoneNumber studentPhone; public StudentImpl(StudentPhoneNumber studentPhone){ this.studentPhone = studentPhone; } @Override public String getStudentDetails() { // TODO Auto-generated method stub return ?Phone : "+studentPhone.getStudentPhone(); } }
In this particular blog we came accross a quick introduction to Spring DI and Spring IOC using Constructor Injection method. In next and final part of the series of Spring IOC tutorial we will cover Spring DI via Setter Method.