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 Setter - Spring IoC

Spring IoC – Dependencies injection Examples

Welcome to third and final part of Spring Dependency Injection tutorial series, in previous article we came across a quick introduction about Spring IOC and Spring DI. Than we came across implementation of Spring DI via constructor method. In this particular blog we will see Spring DI via Getter method in details.

Setter Injection Examples in Spring

Setter Injection is the most preferable method of Dependency Injection in Spring. Setter based DI is implemented by calling a setter method on your Bean. From the configuration point of view Setter Injection is more easier to understand because the property name being set is used as a attribute to bean along with been being injected. See the example below to better understand Setter Injection on User class.

The scenario is that we have two classes, StudentImpl and StudentAddressImpl , each class is implementing an interface Student and StudentAddress respectively. – Interface

Simple interface with a single method declaration.
package com.beingjavaguys.domain;

public interface Student {
 public String getStudentDetails();

StudentImpl – Class

A simple class implementing Student interface to 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 StudentAddress {
  public String getStudentAddress();
} – Class

StudentAddressImpl 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 StudentAddress interface.
package com.beingjavaguys.domain;

public class StudentAddressImpl implements StudentAddress{
 public String getStudentAddress() {
  return "Gurgaon";


Our objective is to call ‘getStudentAddress()’ method of StudentAddressImpl class in StudentImpl class without creating a dependency in between these two classes. This this can be done using setter 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">
  <property name="studentAddress" ref="studentAddressImpl" />

<bean id="studentAddressImpl"  class="com.beingjavaguys.domain.StudentAddressImpl" />

 <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 studentAddress as a property of StudentImpl class. Now this property studentAddress will be used as an object of ‘StudentAddressImpl’ class inside StudentImpl Class.

2) Define ‘StudentAddress’  property in StudentImpl class and make an setter method of that. See the code below :
package com.beingjavaguys.domain;

public class StudentImpl implements Student {

 private StudentAddress studentAddress;
 public void setStudentAddress(StudentAddress studentAddress) {
  this.studentAddress = studentAddress;

 public String getStudentDetails() {
  // TODO Auto-generated method stub
  return "Address : "+studentAddress.getStudentAddress();


In this particular blog we came across a quick introduction to Spring DI and Spring IOC using Setter Injection method. In next and final part of the series of Spring IOC tutorial we will cover Spring DI via Constructor Method.

Thanks for reading !
Being Java Guys Team


Post a Comment

Like Us on Facebook

Like Us On Google+


Skype: neel4soft