Spring Framework

Spring is an open source framework or light weight framework for the development of enterprise applications. Many developers use spring to create high performing, easily testable, reusable code without any lock in. Spring framework works in modular fashion that means it permits to use in parts and leave the other component which is not used by the current application.

Investment and retail banking organizations, well-known dotcoms, global consultancies, academic institutions, government departments, defense contractors, several airlines, scientific research organizations etc are the users of spring. Spring is unique because,

  1. Other frameworks don’t addresses important areas but spring addresses important areas.
  2. Spring is modular and comprehensive.
  3. Spring is a test driven projects. That is the code is write from the ground level.
  4. Spring is an increasingly important integration technology.

The spring framework implements Inversion-of-Control and Aspect Oriented Programming mechanism. The key points here are “layered application framework” and “Inversion of Control and Aspect Oriented Programming.” These can be divided into two broad categories:

  1. Patterns
  2. Components of the Framework

Patterns

The former refers to the design patterns supported by Spring Framework and the latter refers to the components that build up the Framework.

Spring has two patterns at its core. They are:

  • Inversion-of-Control
  • Aspect Oriented Programming

The former is also called IoC and the latter is known as AOP. Here are the details.

Inversion-of-Control is called IoC for short. IoC “is a concept, and an associated set of programming techniques, in which the control flow is inverted compared to the traditional interaction model expressed in imperative style by a series of procedure calls.” In other words, the framework calls the procedures created by the programmer, rather than programmer calling the procedures or methods of the framework. IoC is used to ‘inject’ dependencies at runtime. When one uses this pattern, the dependencies of a particular object are not satisfied at compile time. Instead, the framework provides the required dependencies to the object at runtime. Thus, this pattern releases the programmer from the chore of providing all the dependencies at compile time.

Aspect Oriented Programming or AOP for short, is by definition “An approach to programming that attempts the separation of concerns, specifically cross-cutting concerns, as an advance in modularization.” Here, the key point is separation of concerns. Concern is another term for a module of an application, where module refers to a functionality of the application.

Separation of concerns means that the applications are divided into modules that do not overlap in terms of functionality. However, some functionalities or modules/concerns always overlap other modules/concerns. Logging is an example of such a concern. AOP provides techniques to encapsulate cross cutting or overlapping concerns. In other words, by using AOP a developer can encapsulate cross cutting concerns.

Components

Spring provides both IoC as well as AOP. They are built into the core of the Spring Framework. The next aspect of the Spring Framework worth noting is its support for layered architecture. It provides this support through its various components. Some of them are:

  1. MVC Component
  2. IOC container
  3. DAO support

These three cater to the Presentation, Business, and Persistence layers. Here is a brief overview:

  1. MVC Component: The flexibility of the Spring Framework is that one can use any MVC framework with it. However, it also provides its own framework, called Spring MVC. Spring MVC implements MVC-Model 2 for JSP. It does this by providing controller Servlet and JSP taglib for you to view.
  2. IoC Container: The business logic layer can be developed without coupling it with any other layers by using IoC provided by the Spring Framework. The IoC container is the core component of the framework. IoC lets the developer implement the business logic as Plain Old Java Objects (POJO) and wire them up so that they can be injected or called at runtime. This approach lets you avoid specifying the business objects as dependencies at compile time.
  3. DAO Support: Any of the technologies, including JDBC, Hibernate, and iBatis, can be used with the Spring Framework to access persisted data. The Spring Framework provides this functionality through its DAO support component. A developer can plug in almost any of the persistence framework by using the DAO component.

That completes the bird’s eye view of the Spring Framework. Next, let us have a look at the steps involved in using the Spring Framework, specifically the IoC capability of the Spring Framework.

Architecture of Spring framework

Spring framework is a layered architecture and this architecture consists of seven modules. The modules of spring framework are built on the top of the container, which defines how beans are created, configured, and managed.

The modules in the spring frame work are,

  1. Spring AOP (Source-level metadata SOP infrastructure).
  2. Spring ORM (Hibernate support iBats support JDO support)
  3. Spring Web (WebApplicationContext multipart resolver Web utilities).
  4. Spring DAO (Transaction infrastructure JOBC support DAO support).
  5. Spring context (Application context UI support validation JNDL EJB support and remodeling mail)
  6. Spring Web MVC (Web MVC Framework Web Views JSP/Velocity PDF/Export).
  7. Spring core (Supporting utilities Bean container).


 

Spring Framework
Spring Framework

Spring AOP

The one AOP framework is the one of the key components of spring.  The Spring AOP module integrates aspect-oriented programming functionality directly into the spring framework, through its configuration management feature. As a result you can easily AOP-enable any object managed by the spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application. With Spring AOP you can incorporate declarative transaction management into your applications without relying on EJB components.

Spring ORM

The Spring framework plugs into several ORM frameworks to provide its Object Relational tool, including JDO, Hibernate, and iBatis SQL Maps. All of these comply with spring’s generic transaction and DAO exception hierarchies. The ORM package is related to the database access.

Spring Web

The Web context module builds on top of the application context module, providing contexts for Web-based applications. The spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects. This module is a part of spring web application development stack, which includes Spring MVC.

Spring DAO

The Spring JDBC DAO abstraction layer offers a meaningful exception hierarchy for managing the exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code you need to write, such as opening and closing connections. Spring DAO’s JDBC-oriented exceptions comply with its generic DAO exception hierarchy. So we can say that the DAO support in spring primarily for standardizing the data access work using the technologies like JDBC, Hibernate or JDO.

Spring Context

It is a configuration file that gives context information to the spring framework. The spring context consists of enterprise services such as JNDI, EJB, e-mail, internalization, validation, and scheduling functionality.  This package creates on the beans package to add support for message sources and for the Observer design pattern, and the ability for application objects to obtain resources using a consistent API.

Spring Web MVC

The Model-View-Controller (MVC) framework is a full-featured MVC implementation for building Web applications. The MVC framework is highly configurable via strategy interfaces and accommodates numerous view technologies including JSP, Velocity, Tiles, iText, and POI.

Spring Core

The core container provides the essential functionality of the spring framework. A primary component of the core container is the BeanFactory, an implementation of the Factory pattern. The BeanFactory applies the Inversion of Control (IOC) pattern to separate an application’s configuration and dependency specification from the actual application code

Advantages of Spring Framework

  • Transaction Management: Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring’s transaction support is not tied to J2EE environments and it can be also used in container less environment.
  • JDBC Exception Handling: The JDBC abstraction layer of the spring offers a meaningful exception hierarchy which simplifies the error handling strategy.
  • Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with hibernate, JDO and iBATIS.
  • AOP framework: – Spring is a best AOP framework.
  • MVC Framework: Spring comes with MVC web application framework, built on core spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.