Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Monday, 20 May 2013

Spring Dependency Injection using Constructor - Spring IoC

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.


The scenario is that we have two classes, StudentImpl and StudentPhoneNumberImpl , each class is implementing an interface Student and StudentPhoneNumber respectively. – 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 {

 public String getStudentDetails() {
  // TODO Auto-generated method stub
  return null;
} – 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();
} – 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 {

 public String getStudentPhone() {
  // TODO Auto-generated method stub
  return "8767567546";


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.


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 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;

 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.

Thanks for reading !
Being Java Guys Team


  1. Hi!,
    Bro, you have serveral "typos". You have "getStudentAddress()" instead of "getStudentPhone()" in serveral places. Its a bit confusing.

    Good post!

    1. Hi Julio, Thanks for noticing
      I have made required changes :)


Like Us on Facebook

Like Us On Google+


Skype: neel4soft