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
The configuration XML for constructor based injection for the same scenario is as follows.
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