Saturday, November 20, 2010

Spring core and differences between constructor and setter injection..

There may be lots and lots of Spring tutorials online, but i thought writing a blog like this would be helpful for someone to understand the core concepts of Spring with small examples. People please feel free to pass your comments, so that i can resolve the same in my future technical blogs.

Instead of giving too many theoretical explanations, i have chosen to give a lot of practical examples so that people would find it pretty easy to grasp the core concepts of Spring. We are not going to discuss about the Aspect Oriented Programming support in Spring. This blog covers only the core concepts of Spring. The examples have been given just to make a novice understand about the different concepts of Spring. These classes may not be 100 % object oriented.

Now, let s move into the topic.

Spring is a light weight framework that addresses each tier in a web based application. It provides support for Dependency Injection in the business layer, Spring MVC in the presentation layer and it provides template support for the ORM frameworks like Hibernate, Ibatis etc.,.

Spring framework comprises of several modules. The below given image explains the different modules available as part of the Spring framework. Spring core forms the base for all these modules.

Spring core has two important concepts.

1. Dependency Injection.
2. Aspect Oriented Programming.

What is Dependency Injection???

Instead of objects instantiating other objects, the dependent objects are added by the Spring IOC (Inversion Of Control or Dependency Injection) container based on XML configuration. This happens at run time. It promotes loose coupling and thus prevents hard coding of object creation or lookup.

Now let us look at an example for this.

In this example, this Player class has two instance variables which are meant for holding the player name and the game which that player plays. Here in this case, the Player is meant only for playing Cricket and there is no possibility to change the type of the game he can play. This is one simple example for Tight coupling.







How to avoid this using Spring ?????

Spring supports a good programming practice called "Programming to an Interface". Such a practice makes sure that any subtype of the interface reference can be injected at run time by the container. What we are going to do is to re design the above mentioned classes using this practice to promote loose coupling.

Have a look at the below given examples.











The Player class has been redesigned in such a way that the previous "Cricket" instance variable reference has been replaced by "Game" interface reference so that we can inject any of "Game" interface subtype to the "Player" instance during run time. Now let us see how dependency injection for this example is handled using Spring.

The process of Dependency Injection can be carried out by using Spring in two ways. One by using the setter methods of instance variables and the other is by using Constructors. The configuration XML for setter method based injection is as follows

An instance of a class has to be declared in the XML using the tag. Here in this case, we have created instances for the classes Cricket and Hockey. The tag has attributes like id, class, name etc,. The id attribute will be used to fetch the instance of the bean during run time from the container. The class attribute will be used to specify the class type of the instance that is going to be created. The tag is a child of tag and it will be used to map the values or references to the instance variables of the pertaining class the bean tag is referring to. The tag attribute name will be used to refer the instance variables that particular class has, the value attribute will be used to assign a literal and ref attribute will be used to assign references to the instance variables. In this case, an instance of the Player class will be created and the container will assign a value "Dhanraj Pillai" for the playerName instance variable and assigns a reference of hockey for the game. The classes Hockey and Cricket implements the Game interface, so the container maps the hockey bean reference to the game instance variable of the Player class. Hope this makes things clear about how to promote loose coupling using Spring.

The configuration XML for constructor based injection for the same scenario is as follows.

The configuration changes between the setter method based injection and constructor based injection is minimal. This configuration XML expects the player class to have a constructor with two parameters, one for the playerName and the other one for the type of game the player will play. We are injecting Sachin as the name of the Player and Cricket as the game he can play.


This marks the end of the explanation for the difference between the constructor based injection and setter method based injection.

Catch u guys in the next post...

No comments:

Post a Comment