Understanding Spring AOP

Spring AOP (Aspect Oriented Programming) is one of the key features of the Spring Framework. While Spring’s DI (Dependency Injection) decouples the application objects, Spring AOP decoupled application-wide concerns such as transactions, security, and logging from the objects in which they are applied.

What is Aspect-Oriented programming?

As stated earlier AOP decouples application-wide concerns or cross-cutting concerns. Cross-cutting concerns means any functionality that effects multiple points of an application. Logging, Security and Transaction managements are example of cross cutting concerns.

Cross-cutting concerns - Understanding AOP - Prasadct.com
Cross-cutting concerns – Understanding AOP – Prasadct.com
AOP Increase modularity by separation of cross-cutting concerns from business logic - Understanding AOP - Prasadct.com
AOP Increase modularity by separation of cross-cutting concerns from business logic – Understanding AOP – Prasadct.com

In order to understand further, you have to familiarize with some jargon related to AOP.

Aspect

Aspect is the cross-cutting concerns that we need to modularization. For example logging, transaction management and security are aspects.

Advice

Advice is the concrete implementation of Aspect. It defines what is the job of aspect and when to perform it.

Types of advice

  • Before – Advice functionality executes before the advised method is invoked.
  • After – Advice functionality executes after the advised method is invoked.
  • After throwing – Advice functionality executes after the advised method throws an exception.
  • After returning – Advice functionality executes after method return successful result without an exception.
  • Around – Advice functionality executes before and after the advised method is invoked.

NOTE: Spring supports only method level aspects. We only can use the above Advice types for the methods. Before, After, Around or After throwing an exception from a method.

Joint points

Join points are the places where your aspect functionality can be plug in to the application flow. This is usually a method being called or an exception being thrown.

Point cuts

Point cuts defines on which joint points the aspect needs to apply. This is decided by the point cut expression associated with advice which is usually a method name or regular expression

In Spring all the Advice is written in Java but the point cuts and joint points can be defined using XML or Annotations.

import org.aspectj.lang.annotation.*;

@Component
@Aspect
public class CustomerAspect {
    @Pointcut("execution(* com.prasadct.aop.aopexample.model.Customer.getName())")
    private void customerGetName(){}

    @Before("customerGetName()")
    private void beforeGetName(){
        System.out.println("Before getName()");
    }

    @After("customerGetName()")
    private void afterGetName(){
        System.out.println("After getName()");
    }

    @Around("customerGetName()")
    private void aroundGetName(){
        System.out.println("Around getName()");
    }

    @AfterReturning("customerGetName()")
    private void afterReturnGetName(){
        System.out.println("After return getName()");
    }

    @AfterThrowing("customerGetName()")
    private void afterThrowing(){
        System.out.println("After throwing exception");
    }
}
Spring AOP example - Understanding AOP - Prasadct.com
Spring AOP example with Annotations

How Spring aspects internally works?

In Spring, aspects are implemented as proxies that wrap the targeted method. Since spring creates proxies at runtime, proxies objects will be created when it loads from BeanFactory.

Understanding Spring AOP

It is really important to understand above basic concepts of Spring AOP before going to implement to your own projects. I didn’t included XML based AOP example because annotation based configuration is simple and there is no need to go for XML based configurations with these new Spring-boot features.

3 thoughts on “Understanding Spring AOP

Leave a Reply

Your email address will not be published. Required fields are marked *