Spring Framework - Session 1

22 Jun 2015

Spring Framework

Spring is a lightweight framework. It can be thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc. The framework, in broader sense, can be defined as a structure where we find solution of the various technical problems.

The Spring framework comprises several modules such as IOC, AOP, DAO, Context, ORM, WEB MVC etc. We will learn these modules in next page. Let's understand the IOC and Dependency Injection first.

Inversion Of Control (IOC) and Dependency Injection

These are the design patterns that are used to remove dependency from the programming code. They make the code easier to test and maintain. Let's understand this with the following code:

  1. class Company{  
  2. Projects projects;  
  3. Company(){  
  4. projects=new Projects();  
  5. }  
  6. }  

In such case, there is dependency between the Employee and Address (tight coupling). In the Inversion of Control scenario, we do this something like this:

  1. class Company{  
  2. Projects projects;  
  3. Employee(Projects projects){  
  4. this.projects=projects;  
  5. }  
  6. }  

Thus, IOC makes the code loosely coupled. In such case, there is no need to modify the code if our logic is moved to new environment.

In Spring framework, IOC container is responsible to inject the dependency. We provide metadata to the IOC container either by XML file or annotation.

Advantage of Dependency Injection

  • makes the code loosely coupled so easy to maintain
  • makes the code easy to test

Labels


Java - Spring Framework