Introduction To Spring Framework
Spring is an open source application framework and IOC (inversion of control) container for the Java platform . The first version of Spring was written by Rod Johnson and released in October 2002. In this tutorial we will be using Spring 3.0, which was released in December 2009 .
Spring is used as an alternative to Enterprise JavaBean model and does not enforce any programming model .
Spring framework has independent layered architecture, it is up-to you which component you want to use. Spring provides integration support for different frameworks like ORM and MVC .
How Spring came into picture :
Earlier JavaBeans technology was used in development of Java applications and was intended to be used as reusable components. JavaBeans were good for simple general-purpose applications. Complex enterprise applications that requires more functionality like security and transaction management JavaBeans were not sufficient.
So to address the complexity of enterprise application, Enterprise JavaBeans (EJB) was evolved. EJB provided all the intended functionality but failed to achieve the simplicity of application development. EJB applications were very complex so harder to maintain.
So a framework that provides the EJB functionality and simplicity of application development was needed. Spring provides programing techniques like AOP (Aspect Oriented programing) and DI (Dependency Injection) ; these techniques used with POJOs (Plain-Old Java Object) results in implementing enterprise functionality without EJB Complexity.
The core features of the Spring Framework can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. Although the Spring Framework does not impose any specific programming model, it has become popular in the Java community as an alternative to, replacement for, or even addition to the Enterprise JavaBean (EJB) model.
Concept of DI and IOC :
Spring helps in creating loosely coupled application because of Dependency Injection. Dependency injection is one of the most important concept of spring. In spring objects define their associations (dependencies) and do not worry about how to get those dependencies ; now it is the responsibility of Spring to provide the required dependencies for creating objects. Spring creates the dependent objects and inject those into the bean.
For example : Suppose we have an object Employee and it has a dependency on object Address. So we define a bean corresponding to Employee where it will define its dependency on object Address. When Spring try to create an Object Employee it sees that Employee has a dependency on object Address so first it will create the Address object (dependent object) and then inject this into the Employee Object.
Inversion of Control (IOC) and Dependency Injection are used interchangeably. IOC is achieved through DI. DI is the process of providing the dependencies and IOC is the end result of DI. By DI the responsibility of creating objects is shifted from our application code to Spring container hence the phenomenon is called IOC.
Dependency Injection can be done by setter injection, constructor injection etc . These different types of injections will be explained with the help of example later.
Spring a Container and a Framework :
Spring is treated as a container as it holds the application objects and takes care of complete lifecycle of objects. It manages the dependencies , configuration and scope of objects throughout the application. Spring controls the way in which objects are created , linked with other objects and destroyed.
It is a framework to develop enterprise application in a declarative fashion . Spring can integrate the application with ORM frameworks like Hibernate, iBatis and MVC framework like Jakarta Struts. It provides enterprise services such as JNDI, EJB, e-mail, internalization, validation, transaction and AOP management .
Benefits Of Spring :
- Spring is relatively light weight container in comparison to other J2EE containers. It does not use much memory and CPU cycles for loading beans, providing services like transaction control , AOP management, JDBC interaction.
- Spring makes it easy to develop J2EE application as it has built-in support for WEB MVC framework.
- Spring helps creating loosely coupled applications by DI.
- Spring has no dependency on any application servers.
- Spring creates objects lazily which helps in developing light weight applications.
- Spring supports aspect oriented programming to manage logging, transaction, security etc.
- Spring makes Database interaction (operations) easier as it has support for data access techniques such as DAO, JDBC, Hibernate, IBATIS, JDO etc.
- With the help of spring application code can be unit tested easily as Spring provides unit testing support classes.
- Spring does not need unique deployment steps.
- Spring configuration is done in standard XML format which easy to write and understand.
0 comments:
Post a Comment