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

In this particular blog we will discuss a very important feature of Spring i.e. IoC in spring or Spring Dependency Injection. Spring IoC so called spring inversion of control is a technique that is used to implement dependency injection in Spring.

Dependency Injection or simply Spring DI is a Software Design Pattern that removes hard coded dependencies and make it possible to change them at Compile time or Run Time. IoC Container implements this Dependency Injection design pattern in Spring framework.

Spring IoC (Inversion of Control) is the core of Spring framework. The Inversion of Control manages Java objects from their creation to destruction and make them loosely coupled.

Packages, Classes and Interfaces related to Spring IoC

org.springframework.beans and org.springframework.context are two packages that contains basic functionality of Spring’s IoC.

The BeanFactory interface provides an advanced high level configuration mechanism capable of managing any type of objects. BeanFactory provides configuration level basic functionality for IoC implementation.

ApplicationContext is a sub-interface that is built on BeanFactory, it provides application level context to be used in Spring web applications. ApplicationContext is a complete super-set of BeanFactory and any functionality provided by BeanFactory is also available in ApplicationContext.

Difference between BeanFactory and ApplicationContext

A number of the developers seems to be confused in between the usage of these two Interfaces. BeanFactory interface is the core IoC interface of Spring, it just instantiate and configures the beans. ApplicationContext on the other hand is build on BeanFactory interface so it does everything as BeanFactory and some extra industry-centric functionalities as well. So ApplicationContext is a better choice in most of the situations.

Configuring Beans in Spring IoC container

Spring IoC container consumes some sort of configuration mostly in the form of XML, this configuration tells the container a way in which the objects are to be instantiated, configured and managed in the application.

Spring configuration contains at least one bean definition that the container must manage. These beans are declared under <bean>..</bean> tags and all beans resides under <beans>..</beans> tags.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

  <bean id="..." class="...">
    <!—bean definition  here -->

<bean id="..." class="...">
    <!—bean definition  here -->
  <!-- more bean definitions go here... -->


A bean is simply a Java object that is instantiated, configured and assembled in application by IoC container, there is nothing more than that about the beans we are talking here.

Using IoC Container to manage Dependencies

Every real time Java Application have at least a handful of objects, these objects communicates with each other throughout the application. These objects have dependencies on each other, the role of IoC is actually to inject those dependencies.

There are two general purpose injecting strategies in Spring IoC, these are listed below :
1) Setter Injection
2) Constructor Injection

Spring IoC – Dependencies injection Examples

1) 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.

Changes in spring-context.xml

<bean id="studentImpl" class="com.beingjavaguys.domain.StudentImpl">
<property name="studentAddress" ref="studentAddressImpl" />

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

Changes in Java Class

private StudentAddress studentAddress;
 public void setStudentAddress(StudentAddress studentAddress) {
  this.studentAddress = studentAddress;
To know more about setter injection in spring Click Here.

2) 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.

Changes in spring-context.xml

<bean id="studentImpl" class="com.beingjavaguys.domain.StudentImpl">
<constructor-arg ref="studentPhoneNumberImp" />

<bean id="studentPhoneNumberImp" class="com.beingjavaguys.domain.StudentPhoneNumberImp" />

Changes in Java Class

private StudentPhoneNumber studentPhone;
 public StudentImpl(StudentPhoneNumber studentPhone){
  this.studentPhone =  studentPhone;

To know more about constructor injection in spring Click Here.

In this particular blog we come across a quick introduction to spring dependency injection. We also knows a little about Spring IOC and its implementation using setter method and constructor method. In upcoming blogs we will see Spring DI via Setter Method and Sprng DI via Constructor methods in details.

Thanks for reading !
Being Java Guys Team

1 comment:

  1. I love wiring things together through XML. Rather than annotation. This simplest sample still stands today


Like Us on Facebook

Like Us On Google+


Skype: neel4soft