The Spring Framework: Understanding IoC

IoC or Inversion of Control is one of the core features of Spring. It helps in simplifying the implementation of business logic. To use the Spring Framework to its full potential, understanding the IoC container of the framework is essential. Hence, in this discussion, the focus will be on the IoC – the concept as well as the container provided by Spring Framework.

The first section will focus on the concepts of IoC, including its relationship with Dependency Injection. The focus of the second and third sections will be on the steps necessary to use IoC services provided by the Spring Framework. In the last section, an application will be developed that is based on the concepts explained in first section and whose steps are detailed in the second and third sections. That sets the agenda for this discussion.

What is Inversion of Control?

Inversion of Control or IoC is one of the techniques used to wire services or components to an application program. By definition, IoC is “A software design pattern and set of associated programming techniques in which the flow of control of a system is inverted in comparison to the traditional interaction mode.” Simply stated, in IoC, instead of an application calling the framework, it is the framework that calls the components specified by the application.

This approach is similar to the one that Hollywood agents adopt with their clients. It is sometimes known as “Don’t call me, I will call you.” This is why IoC is also known as the Hollywood approach.

However, IoC is a broad and generic term. The aspect of IoC that the Spring Framework uses is "Injection of required resources or dependency at Run-time into the dependent resource," which is also known as Dependency Injection. Hence, the service provided by the IoC container of Spring is Dependency Injection. Therefore, I will be using the terms IoC and Dependency Injection in a lax way.

There are three forms or styles of Dependency Injection. They are:

  1. Constructor Injection

  2. Setter Injection

  3. Interface Injection


Of these, the Spring Framework directly supports the first and second forms whereas the third form is supported indirectly. Between the first and the second, the Spring Framework prefers the use of second rather than the first. Here are the details.

  1. Constructor Injection: In Constructor Injection, an IoC container uses the constructor to inject the dependency. All the dependencies, whether they are simple values or references to other objects, are declared in the constructor. One of the advantages of Constructor Injection is that all the dependencies are declared in one go. This also helps in understanding whether the class depends on too many services.

  2. Setter Injection: This form of Dependency Injection uses Setters, also known as mutators (because they change the value of the corresponding instance variables), to inject the required resources or dependencies. In other words, each of the objects that the class depends upon will have a setter and the IoC container will use the setters to provide the resource at run-time.

    The main difference between Constructor Injection and Setter Injection is that in Constructor Injection, the handing over of the dependencies takes place during instantiation of the dependent object, whereas with Setter Injection, it takes place after the dependent object is instantiated. The Spring Framework favors Setter Injection over Constructor Injection.

  3. Interface Injection: Interface Injection provides the concrete implementations of an interface to the dependent object according to the configuration. The main difference between Interface Injection and the previous two is that in Interface Injection, any of the implementations of the interface can be injected, whereas with the other two, the object of the class specified is injected. Spring does not provide direct support for Interface Injection.


That completes the overview of IoC. Now let us see how to use each of these forms in Spring Framework.

{mospagebreak title=Spring Framework and Forms of IoC: Implementation}

The central aspect of the Spring Framework is the configuration file, as all the details of the POJOs as well as connecting them with the required services is done in the configuration file. Using different forms of IoC is also no exception. The declarations in the configuration file decide which form of Dependency Injection the container uses.

This section will focus on the required declarations in the configuration file. The POJO that will be used throughout this section will be a simple greeting bean that will get the user’s name and return the string “Greeting” added to the user name. The GreetingBean class is as follows:


public class GreetingBean

{

private String userName;

public GreetingBean()

{


}

public GreetingBean(String a)

{

userName=a;

}

public String sayhello()

{

return “Greetings “+userName;

}

public void setUserName(String a)

{

userName=a;

}

}

Now let us see how to use the different forms of Dependency Injection with Spring Framework.


Constructor Injection

To use constructor injection, the required declaration is done in the bean section of the configuration file that is the beans.xml file. The steps to use Constructor Injection are setting the constructor declaration and passing the value to the constructor.

Setting the constructor declaration tells the container that the application wants to use the Constructor Injection. The declaration is done using the <constructor-arg> child element of <bean> element. For example, to use the Constructor Injection for a POJO named GreetingBean the code would be:


<bean id=”GreetingBean” class=”org.me.GreetingBean”>

<constructor-arg>

:

:

</constructor-arg>

</bean>


The next step is to pass the value to the constructor. To do this, the <value> element of the <constructor-arg> has to be used. The value to be passed is given as the value of the <value> element. For example, to pass “Raj” as username, the code would be: 

<bean id=”GreetingBean” class=”org.me.GreetingBean”>

 <constructor-arg>

<value>Raj</value>

</constructor-arg>

</bean>


That completes the steps for using Constructor Injection. Next we will look at how to use the Setter Injection.

{mospagebreak title=Injections}

The Setter Injection is the most commonly used form of Dependency Injection in the Spring Framework. To tell Spring that the application wants to use a Setter Injection, the declaration needs to be made in the configuration file. Just as with Constructor Injection, the declaration is done in the beans section. The steps involve declaring the properties of the POJO and passing the value to the property. 

The properties of a POJO are its instance variables. To declare the attributes, the <property> element is used. This element is the child element of the <bean> element. The value of the name attribute of <property> accepts the name of a property of the POJO declared using the <bean> element. For example, to declare the userName property of the GreetingBean, the code would be:


<bean id=”GreetingBean” class=”org.me.GreetingBean”>

<property name=”username”>

:

:

</property>

</bean>

 

To pass the desired value to the property, the <value> element needs to be used. It is the child element of <property>. For example, to pass the value “Raj” to the property userName, the code would be:

<bean id=”GreetingBean” class=”org.me.GreetingBean”>

 <property name=”username”>

<value>Raj</value>

</property>

</bean>

 

That’s how Setter Injection is used in Spring Framework.

Finally, the Spring Framework does not support Interface Injection. However, using an auto-wiring service, one can make use of Interface Injection. How to use it will be discussed in the future, when I cover auto-wiring in detail.

That completes this section. In the next section will look at an application that will make use of Constructor Injection.

{mospagebreak title=Spring Framework in the Real World}

The application I will develop in this section is a Compound Interest calculator. The values to be calculated will be "injected" via Constructor Injection. The application consists of three files:

  1. CompoundInterestBean – The Java class with the compound interest logic.

  2. beans.xml – The configuration file for Spring.

  3. Client – The client class that calls the CompoundInterestBean.


Let’s start with the class for the CompoundInterestBean. Just as the name suggests, it is a bean with setters and getters for Principle, Rate and Time. There are two extra methods: calculate, that calculates the interest; and getInterest, that returns the interest calculated.


package org.me;

class CompoundInterestBean {


float years;

float principle;

float rate;


CompoundInterestBean(){

}


public void setYears(float years){

this.years=years;

}


public float getYears(){

return years;

}

 

public void setPrinciple(float principle){

this. principle = principle;

}


public float getPrinciple(){

return principle;

}

 

public void setRate(float rate){

this. rate=rate;

}

 

public float calculate(){

return (float)((principle*(Math.pow(1+(rate/100)),time))-1);

}


public float getInterest(){

return calculate();

}

 

}


Next comes the beans.xml. It contains the declarations for the constructor injection. Since there is more than one argument, there are multiple <constructor-arg> elements.


<beans>

<bean id=”CompoundInterestBean”

class=”org.me. CompoundInterestBean”>

<constructor-arg>

<value>10000.00<value>

</constructor-arg >

<constructor-arg >

<value>10.00<value>

</ constructor-arg >

<constructor-arg>

<value>9.50<value>

</constructor-arg >


</bean>

<beans>


Next is the client class that calls the bean to calculate the interest.


import java.io.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;


public class Client

{

public static void main(String args[]) throws Exception

{

try

{

System.out.println("please Wait.");

Resource res = new ClassPathResource("beans.xml");

BeanFactory factory = new XmlBeanFactory(res);

CompoundInterestBean interest=

(CompoundInterestBean)factory.getBean(“CompoundInterestBean”);


System.out.println(interest.getInterest());

}


catch(Exception e1)

{

System.out.println(""+e1);

}

}

}


To run this application successfully you will need the Apache Commons Library. 

That completes this discussion on how to use the forms of IoC with the Spring Framework. However, it opens up certain other questions. For example, can the IoC work for accessing data from database or other data sources? These questions will be the focus of discussion in the next part. Till then…

[gp-comments width="770" linklove="off" ]

chat