Wednesday, September 4, 2013

Spring, AOP and Hibernate: Understand Spring

Removed

Advantages of layered (multi-tier) architecture :


  • Improved modularity and reuse 
  • Simplifies development
  • Simplifies testing


Layers are essential because they separate some concerns such as presentation and business logic. But without the business tier, there are concerns that are not easily separated. They are called cross cutting concerns such as transactions, security, persistence, logging, etc.
These cross cutting concerns span multiple application components and they must be implemented by sprinkling code throughout the application.

What is Spring


It is a framework for simplifying Java EE plateform application development and enforce the respect of multi-layered development.
By using spring, the code is supported by a backbone that handles dependency wiring, provides aspect-oriented programming (AOP) capability, and generates cohesive configuration metadata that implicitly documents the way your application's pieces fit together.
Spring has enabled the externalization of integration details to an XML configuration file or to express dependency injection through Java annotations.
The mission of spring was first to make the development of Server-side Java applications simpler. It offered a better solution to wiring application dependencies together. For this reason, Spring is often referred to as a Container, meaning that it offers a centralized abstraction for integrating collaborating dependencies via configuration, rather than writing code to handle this task.
Spring's job is to parse your configuration files and then instantiate your managed classes, resolving their interdependencies. The BeanFactory interface defines the core Spring engine that englobe your beans and wires the collaborating dependencies together.
The ApplicationContext extends the BeanFactory interface, providing a set of more robust features. Spring ships with a listener that you can throw into your web.xml file to automatically bootstrap the Spring ApplicationContext and load your configuration file.

Beyond its original IoC roots, Spring provides a pluggable transactional management layer, AOP support, integration points with persistence frameworks, and a flexible web framework called Spring MVC.

Persistence Layer- Spring Hibernate:

The persistence layer often serves as the foundation upon which an application is built. Spring helps to enforce a modular architecture in which the persistence tier is devided into several core layers that contain the following components: 
  • The domain model
  • The Data Access Object (DAO) layer
  • The service facade (or Service Façade)
The domain model represents the entities within an application, defining the manner in which they relate to one another. Each class within the domain model contains the various properties and associations that correlate to columns and relationships within the database.

The DAO layer defines the means for saving and querying the domain model data. The goal of the DAO pattern is to completely abstract the underlying persistence technology and manner in which it loads, saves, and manipulates the data represented by the domain model. The key benefit of the DAO pattern is separation of concerns- the lower-level details of the persistence technology and datasource are abstracted into a series of methods that provide querying and saving functionality. If the persistence technology changes, most of the necessery change would be limited to defining a new DAO implementation, following the same interface. Defining DAO methods in a separate interface is crucial, as it decouples the purpose and contract of the DAO from the actual implementation. Of course, the DAO implementation class will use Hibernate, JPA or whatever persistence technology we have chosen to employ. However, the higher layers of the application are insulated from these details by the DAO interface, giving us portability, consistency, and well-tiered architecture. 

The layer that handles the application business logic is called the Service layer.  It defines an application's public-facing API, combining one or more lower-level DAO operations into a single, cohesive transactional unit. Service facade methods group together multiple DAO methods to accomplish business logic as a single unit of work: this is the concept of a transaction
In Spring, the service layer typically is intended to accomplish three primary tasks:
  • Serve as the core API throught which other layers of your application will interface
  • Define the core business logic, usually calling on one or more DAO methods to achieve this goal
  • Define transactional details for each facade method  


Dependency Injection in Spring:

Spring has become the facto standard for integrating disparate components and frameworks, and has evolved far beyond its dependency injection roots. The purpose of dependency injection is to decouple the work of resolving external software components from your application business logic.
Spring is a lightweight IoC container, meaning that it will assume the responsibility of wiring your application dependencies. Spring allow to tie components together in a loosely coupled manner. This has far-reaching efforts for any application, as it allows code to be more easily refactored and maintained.

Aspect Oriented Programming AOP:

AOP is a strategy that allows behavior to be injected into code in places across an application (a whole slew of classes). The functionality that we would like to apply across the series of classes has little to do with the main purpose of those classes.
Spring ships with transactional support that can be applied to application code through the use of interceptors that enhance your service layer code. An interceptor is code that can be mixed into the excution flow of a method, usually delegating to the interceptor befor and/or after a particular method is invoked. The interceptor encapsulates the behavior of an aspect at a point in a method's execution.
At the basic level, Spring accomplishes AOP through the use of the proxy design pattern.


When you advise your classes by injecting cross-cutting behaviour into various methods, you are not actually injecting code throughout your application (although in a way, that is the net effect of using AOP). Rather you are requesting that srping create a new proxy class, in which functionality is delegated to your existing class along with the transactional implementation.
When you weave cross-cutting behavior into your classes via AOP, Spring is not directly inserting code; rather, it is replacing your classes with proxies that contains your existing code intertwined with the transactional code. Now applying transactional behaviour to a service layer class is a matter of specifying the @Transactional annotation at either the class or method level.


Biblio:
- Book: Spring persistence with Hibernate, Paul Tepper Fisher and Brian D.Murphy

1 comment :

Articles les plus consultés