Saturday, 10 November 2012

Lets Play with Spring Framework

Hi guys,now we are going to learn about Spring Framework .
Spring Framework is a open source framework so don't worry about money...hmm just kidding.So whats new in Spring lets talk about it...
Spring give us few features which are  really very interesting. but two of them means a lot.
1 . Loose Coupling, and
2 . Dependency Injection.
So lets discuss about each of them.
Since we are using MAVEN for the application development so please add the following dependency in your pom.xml file.

Loose Coupling:


For understanding loose coupling here we took an Example from our conventional JAVA programming. Suppose we have two classes Indian and Car such as :
Our Indian Class is such as:
 Our Car Class:

 Here we can see that  in our Indian class we are creating an instance of Car class by using the new operator and if somehow we wants to change our Car object  by any other object such as Bike , Bus , Motor  or any object than we need to change in our JAVA class which is not a better idea. We have to put Bike or anything else in place of Car. Such kind of situation is called tight coupling between the classes.Our Person class is tightly coupled with Concrete Car class.So Here Spring provides a better solution for that.Lets have a look:

Spring came with introducing a xml file.It is very simple to make a beam of a class and than call it in our Indian class such as:

 We write a ApplicationContext which tell us about the xml file  and by simply using refrence of this application context we can use our bean of car class by its ID name.So here we can see that there is no need to create a Car object by new operator in our Indian class.This work is now done by our xml file.But Still we are not getting fair enough results.So if we wants some more flexible and loosely coupled classes it is always a better idea to use Interface, for example if we want to use Bike move method instance of Car than first we should make a Interface Vehicles which was implemented by both of our Classes Car and Bike.
Indian Class :

Now if we look our Indian class than we can see that if we want to change our Class from Car to Bike than there is no nee to change in JAVA file. we can achieve it only by changing in XML file such as:

So by using Spring we can make our JAVA classes move independent and loosely coupled.
Here by using xml configuration we get the loose coupling.

Now have a look on second advantage:

Dependency Injection :


Suppose we have a person class which have a property name which was inherited from name class but for that we again create name class object and than we use that object for getting name.Spring came here with a solution so that there is no need to use the new operator in our JAVA class.
So here is our solution :
1.First we make a Interface of name which have a method   myName() which is used by a class DisplayThisName() and provides its implementation.
Name Interface:
DisplayThisName class:

2.Now in Our Person class we write the setter and getter methods for the values of name.
Person Class:

3.Now write the main class for executing the our application.Here is our main class:
 Main class:
 4.After writing JAVA classes we configure the xml files for all classes.As:

This is all about the loose coupling and dependency injection later on we will discuss on bean wiring with annotation.

No comments:

Post a Comment