Learnitweb

Aspect Oriented Programming with Spring – An Introduction

1. Introduction

In every application there are layers such as UI layer, controller layer, service layer, DAO layer. The number of layers depends on the design and type of application. Across these layers, there are few aspects which are common. For example, logging is such aspect which is common across layers. Every layer has a typical requirement of logging requests or messages. Transaction and security are another examples of such cross-cutting concerns.

Aspected oriented programming is a way of dealing with cross-cutting concerns which cut across multiple layers, types and objects.

Spring supports two ways of writing custom aspects:

  • Schema-based approach
  • @AspectJ annotation style

Aspect increases modularity by allowing the separation of cross-cutting concerns. AOP allows to add additional behavior to the code without modifying the code itself. Using AOP we write the new code which introduces new behavior separately.

2. AOP terms

  • Aspect: An aspect is a modularization of a concern that cuts across multiple classes. Transaction management and logging are cross-cutting concerns in applications. In AOP, aspects are implemented using either by schema-based approach or @AspectJ annotation style. In Spring, aspects are usual classes.
  • Join point: A Join point is a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring it is a method execution.
  • Advice: Advice is an action taken by an aspect at a particular join point. Different types of advice include “around”, “before” and “after” advice. Spring models an advice as an interceptor. Interceptors can be chained around the join point.
  • Pointcut: A predicate that matches join points. A pointcut expression and an advice are associated. An advice runs at any join point matched by the pointcut. Spring uses the AspectJ pointcut expression language by default.
  • Introduction: Declaring additional methods or fields on behalf of a type. Using Spring AOP, you are allowed to introduce new interfaces and a corresponding implementation to any advised object. For example, you could use an introduction to make a bean implement some interface.
  • Target object: Also referred as the “advised object” is an object being advised by one or more aspects. Since Spring AOP is implemented by using runtime proxies, this object is always a proxied object.
  • AOP proxy: An object created by the AOP framework in order to implement the aspect contracts. In the Spring Framework, an AOP proxy is a JDK dynamic proxy or a CGLIB proxy.
  • Weaving: Weaving is linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP performs weaving at runtime.

3. Types of advice

Spring AOP has the following types of advice:

  • Before advice: Advice that runs before a join point but that does not have the ability to prevent execution flow proceeding to the join point unless it throws an exception.
  • After returning advice: Advice to be run after a join point completes normally, for example, if a method returns without throwing an exception.
  • After throwing advice: Advice to be run if a method exits by throwing an exception.
  • After (finally) advice: Advice to be run when a join point exits in both the cases (normal or by throwing an exception).
  • Around advice: Advice that surrounds a join point such as a method invocation. Around advice can execute before and after the method invocation. Around advice lets you choose whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception. It is the most powerful advice of all as it can run before as well as after the method execution.

4. Few more important points

  • Spring AOP is implemented in pure Java and is suitable for enterprise applications.
  • Spring AOP currently supports only method execution join points. AspectJ can be used if you want to advise field access and update join points.
  • If required you can use AspectJ with your spring application. Integration of AspectJ with Spring AOP does not affect Spring AOP API.