01 - Introduction to Spring

1.1 Spring Overview

SJava pring Framework is an open source framework that provides all infrastructure needs for the rapid development of standard and enterprise java applications .

Spring Framework was first released in year 2004.Since then spring has added a lot of new functionality day by day and release by release .

Latest released version of Spring framework is 4.x

Spring Framework has provided a support, features and integration for various technologies like

  • Support for transaction management.
  • Support for interaction with database.
  • support for Aspect Oriented Programming
  • Integration with Object Relationship frameworks like Hibernate , iBatis etc. 
  • Support for dependency injection which means all the required dependencies will be resolved with the help of containers .
  • Support for REST style web services.

Spring Framework is developed with modular approach so it has around 20 different modules and allows us to be selective about the components we need to use so we can just use the module needed (not required to integrate/deploy all modules).

1.2  Advantages of Java Spring Framework

Each and every framework has its pros and cons so is with Spring Framework . Now a days in java /j2ee application development Spring has been widely used because of its numerous advantages . Some of them are -

  1. Spring is lightweight loosely coupled framework developed with modular approach which helps applications to deploy/use only required modules.
  2. POJOs can be used as model as Spring is based on interfaces.
  3. Support for various frameworks like transaction management , schedules , and integration with other popular and powerful frameworks like Hibernate.
  4. For flows based applications Spring provides Web flow which helps in defining the flow of application in xml file instead of code which helps in maintainability of application.
  5. Spring  implements MVC pattern which means provides a clear separation between  model , view and controllers.
  6. Supports interceptors which can be used to execute common functionality like logging. Interceptors are provided in such a way so flow can be intercepted at most useful points like pre/post controller execution as well as post view display.
  7. Latest releases of Spring removes the restriction of implementing any specific interfaces like controller to make any class behave like Controller . Instead we can just use the annotations.
  8. Spring provides several resolvers like jsp, xsl to resolve views.

1.3 Spring Modules Overview

       Spring comes with around 20 modules which are grouped in to

  • Core Containers – Includes Beans, Core, Context and SpEL modules
  • Data Access/Integration- includes JDBC, ORM, OXM, JMS and Transactions modules
  • Web- Includes Web Socket, Servlet, Web and Portlet modules
  • AOP (Aspect Oriented Programming)
  • Instrumentation
  • Messaging
  • Test

1.4 Dependency Injection (DI)

Dependency Injection is at the heart of the Spring framework. The basic concept of the Dependency Injection is that framework provides the dependencies of class itself.

We need not create the objects instead we just define how they should be created and IoC container will create the objects for us.

For example if we have two classes ‘ClassA’ and ‘ClassB’. ClassA is dependent on ClassB then container will inject the instance of ClassB in an instance of ClassA.

This feature is very useful specially in a large and complex applications where it is very important to have the classes loosely coupled. In other words, Dependency Injection allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable.

1.5 Aspect Oriented Programming (AOP)

Logic of any application consists of two parts-

  •  Business Logic – is the actual logic that is intended to achieve the functionality.
  •  Cross Cutting Concerns – is the utility code that can be applied to business logic and required at several parts of the application like logging, transaction management etc.

The basic principle of AOP is finding common tasks which is required at many places in the code and does not belong to the business logic .Aspect Oriented Programming (AOP) provides a way of modularising application logic, so that each module addresses a distinct concern.

For example if we want to write a log statement on entry of every method of our application then instead of having logging logic at several places in an application, AOP provides a means of modularising this logic, and applying it to various parts of the application at runtime. This provides a clear Separation of Concerns and need not to be tied with business logic.

Like us on Facebook