Month: September 2015

Spring Framework in Java

The Spring Framework is an application framework and inversion of control container for the Java platform.
Moreover we can say this is application framework for dependency injection (a design pattern to build decoupled applications).

Let’s simply see why and where to use spring instead of its definition. Understanding these will clear “What it is”?

Let suppose there is a travel agency which provides tours by AIR, water and roads.Below are classes and Interfaces for this use case.

public interface ITour
{
public void setAmount(int amount);
public void displayBookingInfo();
}

public class TourByAir implementes ITour
{
public void setAmount(int amount)
{
//Set charges of Tour by Air.
}

public void displayBookingInfo(){

}
}

public class TourByShip implementes ITour
{
public void setAmount(int amount)
{
//Set charges of Tour by Ship.
}

public void displayBookingInfo(){

}
}

public class TourByRoad implementes ITour
{
public void setAmount(int amount)
{
//Set charges of Tour by Road.
}

public void displayBookingInfo(){

}
}

public class VacationTour()
{
//Create Instance Of ITour.
private ITour tour;
}

Now for an instance of ITour we may require any of TourByAir, TourByShip or TourByRoad. It will be simply  depending on individual’s demand. For this we use Factory Pattern as creational pattern. And by doing some code  for initiating ITour based on multiple criteria’s we will done with our use case.
Spring provides the better way, it allow us to Declare mapping in XML and initialize objects automatically. By  using maximum singleton architecture of instances it helps in memory optimization.

The above is known as “Inversion Of Control”. And this is for what Spring framework is made.Hope this answered what,why,where for Spring.

Now one important this is that components which we write in spring are POJO (Plain Old Java Object) or POJI  (Plain Old Java Interface) means they are not inheriting any class of Spring it self. So the meaning of  “Inversion Of Control” i.e. “The application controls the framework, not the framework controls the application”, is verified in above discussion.

In next Tutorial we will see create sample application with discussed classes using Eclipse IDE.