Spring AOP : Basic concepts

Introduction

AOP (Aspect Oriented Programming), it is a programming paradigm that helps to modularize and manage cross cutting concerns during software development.
Let's understand first what does mean by cross cutting concerns, In AOP cross cutting concerns refers to the part of application code that affect multiple module or component.
For example logging, Transaction handling, Exception handling, Security, etc. All these are cross cutting concerns that are needed at multiple places in application code.
These cross cutting concerns helps for proper functioning of our application, monitoring application issues, handling DB transactions, authentication and authorization. These are scattered all over application, but these are not part of business logic, to isolate it from business logic so that it work as single module and to manage it we need AOP.
So we can say AOP helps in modularizing cross cutting concerns, isolate it from business logic , making application code cleaner and maintainable.

Main Components of AOP

  • Aspect : It encapsulates cross cutting concerns and advice methods. In spring boot we can define it using an annotation @Aspect that we can put over a class. We also need @Component annotation as well over this class so that Spring can scan and manage it.
    Example :
  • Join Point : It refers to the place or point during program execution where aspects advice can be applied. Example, method in class.
  • Pointcut : It helps to define set of joinpoint where aspect's advice can be applied. pointcut uses expression to define condition to filter joinpoint, on which aspect's advice executed.
    Example : "execution(* com.example.service.*.*(..))"
    In spring AOP we have various annotation that uses point cut, some of them are @PointCut used to define named pointcut, @Before, @AfterReturning, etc.
  • Advice : It represent code that run/execute at specific join point. Spring AOP supports different types of annotation that help to define an advice and also tells when it should execute, such as @Before, @After, @AfterReturning, @AfterThrowing, and @Around.

Now let's visualize all these component together in spring boot. Don't worry about code just see how it looks like.

Let's understand what is happening here

  • We defined an aspect in form of class named as LoggingAspect.
  • In LoggingAspect we define a method "public void logBeforeMethodExecution(JoinPoint joinPoint)" and on top of this method we have annotation @Before which tells this method is advice.
  • @Before("execution(public int *.*.aopdemo.service.MyServiceImpl.getSum(int,int))")
    Here the highlighted part is point cut, this point cut expression filters only those join point which are under package aopdemo.service and having class MyServiceImpl and that class must have method getSum(int,int) with 2 integer argument.
  • Second screen shot represent the join point , in this case it is "com.coderstuff01.aopdemo.service.MyServiceImpl.getSum". So it means before this method log will print and then only method will execute.
So, here you can see logging part is isolated from actual code, that's what AOP do.

There is one more important term in spring AOP, it is weaving.
Weaving : It refers to the process of integrating Aspect with main application code. Weaving helps to combine Aspect related code with actual join point during execution of program. It can be during compile time or runtime.

  • compile time : aspect code integrate with join point in core code during compile time with the help of compiler (AspectJ's Compiler).
  • run time : integration of aspect code happens dynamically during runtime. Spring used runtime weaving. Spring uses @EnableAspectJAutoProxy annotation which create proxy around target object and apply the aspect's advice on joinpoint which satisfy the expression condition of pointcut.

Comments

Post a Comment