Thursday, 1 September 2016

Spring Framework: IOC, DI and Bean Configuration

Post Brief Table of Content

  • Introduction
  • What is IOC?
  • Forms of IOC
  • What is DI?
  • What is Spring Bean?
  • What is Bean Dependency?
  • Spring XML Bean Configuration
  • What is Injection?
  • Interview Questions


Please go through my previous post at Introduction to Spring (click on the link). In this post, I'm going to discuss some basic principles of Spring Framework. 
Spring Framework follows IOC or DI Design Pattern. We will discuss some important concepts or questions in this post: What is IOC? What is DI? Types of DI? Advantages of DI etc. Let us start this tutorial now.

What is IOC?

IOC stands for Inversion of Control. IOC is an architectural Design Pattern. Spring Framework was designed based on this pattern. 

IOC means “Developer is not responsible for creating any single object. Container creates all the required objects and given them to the developer”
What are the responsibilities of Developer in IOC?
Developer is responsible for informing the following three things to the Container:
  • What
  • Where
  • When
That means 
What type of objects they want
Where they want those objects
When they want those objects – either Deployment time or Runtime.

Forms of IOC

In this section we will discuss about what are the different forms of IOC available.

In general, IOC has two main forms
  • DI (Dependency Injection)
  • Dependency Bijection
Spring Framework supports only DI, does not support Dependency Bijection. JBoss SEAM framework supports both DI and  Dependency Bijection.
What is DI?

DI stands for “Dependency Injection”. It is one of the forms of IOC Pattern. Spring Framework uses this design pattern extensively in all it's modules.

DI means “Resolving bean dependencies by using Injection”. Resolving bean dependencies done by Spring Container, not by us (Developers).

Like Servlet API uses Servlet Container to run servlets and JSP has JSP Container,  EJB uses EJB Container etc., Spring Framework uses Spring Container to run Spring applications. As Spring Framework follows this IOC or DI Design pattern extensively, we can call it as "Spring IOC Container".

What is Spring Bean?

Spring Framework treats every Java class as a Bean. We need to configure them by using of the following techniques:
  • Spring XML Configuration
  • Spring Annotations
  • Java Config
Spring Bean Example:-
public class CreditCard
  private int cardid;
  private int cardnumber;
  private String custname;
  // Setters and Getters

public class CardService
  private CreditCard card;
  // Setter and Getter

What is Bean Dependency?

Please observe above example carefully. 

Here we have two beans: Two classes – CreditCard and CardService. CardService class is using CreditCard class object (OOP: Composition) That means CardService is depending on CreditCard object to perform card operations.

This is called Bean Dependency or Bean Collaboration.

Spring XML Configuration

In this section, we will see some examples on how to configure Spring Beans using XML Configuration. We will discuss rest of two concepts like Spring Annotations and Java Config in my coming posts.

Simple Bean Declaration Syntax:-
<bean id=”beanObjectName”  class=”FullyQualifiedBeanName”/>
Here id attribute needs a name that is beanObjectName.  it is our instance name. We can give any name we want.

class attribute needs fully qualified class name.

<bean id=”creditCard”  class=””/>
Bean With Properties Declaration:
If Bean contains primitive data type properties or  String properties, we need to use the following syntax. 

<bean id=”beanObjectName”  class=”FullyQualifiedBeanName”>
<property  name=”property-name”   value=”property-value”/>

<bean id=”creditCard”  class=””>
 <property name=”cardid” value=”1001”/>
 <property name=”cardnumber”   value=”2222222”/>
        <property  name=”custname”  value=”Lokesh”/>

If Bean contains reference data type properties, we need to use this syntax 
<bean id=”beanObjectName”  class=”FullyQualifiedBeanName”>
 <property  name=”property-name”   ref=”property-value”/>
Here attribute  “ref”  stands for reference data type.

<bean id=”creditCard”  class=””/>
<bean id=”cardService”  class=””>
 <property  name=”card”   ref=” creditCard”/>
NOTE :-  <property> element attributes
  • name: Bean property name
  • ref: Bean reference name

What is Injection?

In this section, apart from answering What is Injection? we will also answer this important question: How to resolve bean dependencies using Injection?

Please observe above examples. CardService bean has a dependency on CreditCard bean.
Here CardService is referring or using CreditCard bean using ref=””  attribute. This is called Injection.

We are resolving bean dependencies by using Injection.

And we have answered many interview questions in this post right. Let me remind you some important questions here:
  • What is IOC?
  • What is DI?
  • What is Spring Bean Configuration XML file?
  • We use XML file to provide all bean configurations.
  • Why we call Spring Container as “Spring IOC Container”?
That’s it all about “Spring Framework: IOC, DI and Bean Configuration” concepts. We will discuss some more important Spring Basics in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.