Introduction to Aspect oriented programming in spring

article

Spring framework is a popular Java framework that provide support for various requirements of a Java application. It supports dependency injection, web services, MVC pattern etc. The main aim of a framework is to lift the heavy job like data base connectivity, Java web development service calls from developers; so that they can concentrate on developing the business logic of application. Apart from this the framework also aims at reducing the redundant code in an application. This code redundancy occurs when we need to perform same operation across different modules of an application. We call these operations that span across modules as cross cutting concerns.

There are some cross cutting concerns in every application. For example, we may need to log the parameters of methods in all classes. Rather than adding a repeating logging code, we can use the Spring frame work provided AOP (Aspect Oriented Programming) for logging. An aspect can be configured to be executed before method invocation, so the parameters can be logged within the aspect. This tutorial gives an overview of the aspect oriented programming in Spring framework.

Basic Concepts

In Object Oriented Programming, class is the unit of modularity which means a piece of code that is intended to perform a particular function. While in AOP the unit of modularity is Aspect. These aspects enable the modularization of cross cutting concerns in the application. AOP frame work is a key component in Spring frame work. Now let us see some basic concepts in AOP.

  • Aspect: In Spring an aspect is a POJO class that contains the logic which is common across modules. For example a logging aspect contains the code for logging. @Aspect annotation is used mark an aspect.
  • Join point: refers to the point in the application execution where we can apply the aspect. For example we can say the aspect needs to be called when a method invocation happens or exception is thrown.
  • Advice : This says when and where an aspect needs to be invoked. There are "around," "before" and "after" advice types. The actual execution of aspect logic happens here. The before advice applies before the join point.
  • Point cut: Point cuts are specified using expressions or patterns which says where an aspect to be executed. Usually an Advice comes with a Pointcut expression and runs at any Joinpoint matched by the Pointcut.

Maven Dependency

Add below maven dependencies for AOP in your applications pom.xml to use AOP in your Spring project.

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency>

Define an Aspect:

An aspect can be defined using @Aspect annotation in a POJO

@Aspect public class LoggingAspect { }

The above code will declare LoggingAspect POJO as an aspect in your application. This can contain the logic for logging

Define a PointCut:

A point cut is defined using @Pointcut annotation.

@Pointcut("execution(* com.example.myapp.service.*.*(..))") public void logServiceCalls() {}

The above point cut definition indicates the execution of any method in any class of the package com.example.myapp.service, where we are going to apply the aspect. The logServiceCalls() invocation is tied to the execution of methods in this package.

Define Advice

The advices can be added using annotations @Before - , @After, @Around, @AfterReturning, @AfterThrowing etc.

@Before("serviceCall()") public void doBeforeServiceCall(){ // Advice that will be invoked before executing serviceCall() method. } @After("serviceCall()") public void doServiceCall(){ // After Executing serviceCall } @AfterReturning(pointcut = "serviceCall()", returning = "result") public void doAfterReturnningServiceCall(Object result) { // you can intercept result object. ... } @AfterThrowing(pointcut = "serviceCall()", throwing = "ex") public void doAfterThrowingInServiceCall(Exception ex) { // you can intercept thrown exception here. ... } @Around("serviceCall()") public void doAroundServiceCall(ProceedingJoinPoint pjp){ // start time pjp.proceed(); // end time }

The above code shows the use of a JoinPoint in the advice. This gives us the flexibility to control the application execution. A call to proceed() method of JoinPoint will trigger the serviceCall() invocation. The JoinPoint also has methods to get the paramters passed to the point cut method.

Conclusion:

In this tutorial we have seen how the aspect oriented programming can be used to handle cross cutting concerns in an application. AOP allows to modularize these cross cutting concerns and apply it on the method during its execution. For that AOP provides many ways to intercept the method execution. An aspect can be attached to events like before, after method call, after an exception is thrown etc. A lot of boiler plate code can be avoided by the judicious usage of AOP concepts. Thus AOP helps developers to write efficient code with ease.

 
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img
  • img