Monday, 29 August 2016

Introduction To Spring Framework


Post Brief Table of Content

  • Introduction
  • What is Spring Framework?
  • Drawbacks of Non-Spring Applications
  • Why We need Spring Framework?
  • Advantages of Spring Framework
  • Drawbacks of Spring Framework
  • Spring Framework follows Invasive Approach
  • Spring Configuration
  • Pull Model
  • Push Model

Introduction

I'm going to deliver a serious of posts on Spring and Hibernate Frameworks. If you are new to XML, please go through my previous post on XML Basics (click on this link) because both frameworks uses XML extensively. It is my first post in this series.
We need Java and XML Basics to start this tutorial series.

What is Spring Framework

Spring Framework is an open source, light weight and loosely coupled Java Framework for developing enterprise applications very easily.
Founder of Spring Framework: Pivotal Software
Spring Framework Website: https://spring.io/

First version was release by Rod Johnson with his book 
Expert One-on-One J2EE Design and Development in October 2002.

First Spring 1.0 Milestone version was release in March 2004.
Latest Stable Spring Framework version is 4.3.2, released on June 2016

Drawbacks of Non-Spring Applications

Non-Spring Java EE web applications have the following drawbacks:
  • Code duplication or boilerplate code
  • Resource Leakage
  • Need to take care about exception handling
  • Tightly coupled applications
  • Tough to do unit testing
  • Need to write code for Singletons
  • Need to write code for Factory classes
Why we need Spring Framework?
As we discussed in the previous section Non-Spring Java EE Web Applications have lots of drawbacks. That's why most of the Java-based projects are running using Spring Framework.
Spring Framework solves all the above mentioned problems and also provides many benefits (Will discuss in next section).

Advantages of Spring Framework

Spring framework is one of the most popular web applications development framework because of it's benefits:
  • No code duplication
  • No Resource Leakage 
         Framework will take care about resource management 
        (Resource Closing)
  • No need to worry about Exception Handling
         Spring Framework takes care about converting Checked Exceptions   
         (SqlException) into Unchecked Exceptions(DataAccessException)
  • Loosely coupled applications
  • Easy to do unit testing (Spring Unit Testing Framework)
  • No need to write code for creating singletons because it provides declarative approach to create singletons.
  • No need to write code for factory classes. Use POJO style model    
NOTE:- Main Feature of Spring Framework is it follows Invasive Approach
Spring Framework follows Invasive Approach

Spring framework does NOT force the developer to define the bean classes by extending or implementing any Spring Framework related API classes or interfaces.

Spring Configuration

Other MVC Frameworks like Struts, JSF, Wicket etc forces the developer to use only XML file for Framework configuration file.

However, Spring framework never forces the developer to use only XML file
for Spring Bean Configuration file.

Spring supports the following options for Spring Bean Configuration file:
  • XML configuration file
  • Properties file
  • Programmatic Approach
  • Annotations Approach
Pull Model


Non-Spring J2EE Web Applications follow Pull model that means 
Developer is responsible for creating objects and pulls into the container

Example:-
Emp empObj = new Emp();
// Set employee values using Setter methods
httpSession.setAttribute("emp",empObj);



Drawbacks of Pull Model Approach:

Developer is responsible for creating the objects
Increases application complexity
Increases Development time
Tough to do the application unit testing 
Increases components dependency

Push Model

Spring Framework follows Push model approach that means Developer is not responsible for creating any objects. Container creates all required objects and give them to the developer.




Advantages of Push Model Approach:
Developer is not responsible for creating of objects
Reduces the application complexity
Reduces development time
Easy to do the application unit testing
Decreases components dependencies

Why Spring is more popular than other MVC frameworks like Struts, JSF, Wicket?
See "Advantages of Spring Framework" Section 

How Spring resolves all Non-Spring Java EE application problems?
Because of Spring Framework supports the following two concepts:
  • IOC(Inversion of Control)
  • DI(Dependency Injection)
We will discuss about IOC and DI in my coming posts with some useful examples soon.

That’s it all about “Introduction to Spring Framework”. We will discuss some more important Spring Framework concepts in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.
Thank you.