Spring Boot: AOP + AspectJ annotation example

In this tutorial i will show you how AOP works with spring and AspectJ also what all basic things are required to dive into it.

Here I am creating this demo using spring-boot framework and used AspectJ to intercept methods.

AspectJ

AspectJ is a java based cross-platform extension for aspect-oriented programming. It is now available at http://www.eclipse.org/aspectj/.

Mainly it is used to intercept any particular method at runtime. It is used to handle logging, error handling, error checking and for many cross cutting concerns. It has it’s own syntax which is really helpful to do desired task.

If you want to understand all AOP concepts in detail than you need to understand some terms which you can find here.

Basic aspect terms are

  • Aspect
  • Join point
  • Advice
  • Pointcut
  • Weaving

Common AspectJ annotation are listed below, it also known as advice.

  • @Before: Execute before method execution.
  • @AfterReturning: Execute after method execution completes normally.
  • @AfterThrowing: Execute after method throws any Exception.
  • @After: Execute a method if method execution goes normally or throws an exception.
  • @Around: As word around suggest it is combination of all listed below.

Use Case

The sample use case of AOP is let’s say we need to write some business login in method and also want to check whether user has rights to access this method and also we need to log all events. In this case it is better to use AOP instead of writing all 3 things into same method and it helps to make code loosely coupled.

Let’s create one use case so you can easily understand real time use of AOP with spring. Use of AOP is depends on requirement and your use case.

Here i want to log all Setter event which is occurred on my VO (Value Object) also i want to print what setter method is invoked and what is the argument passed to particular setter method.

You can create a new sample boot project or clone below repository.
GitHub Repo: https://github.com/yogeshmprajapati/kode12-spring-boot.git
Module: spring-boot-aop-basic

The details of files are:

pom.xml

You only need to add below dependency into your pom.

Employee.java

SetterMonitor.java

A class contains actual implementation of Aspect and Before Advice using @Before annotation.

@Aspect annotation is used to declare the class as aspect.

@Before annotation

1. @Before the code written in this annotation is executed before calling of actual method
execution(void com.kode12.vo.*.set*(*)) : This is the code which shows where, when and what to execute before a method execution and
2. void : it applies on all method having return type void and
3. com.kode12.vo.*: it applies on all classes which resides under com.kode12.vo package and
4. set*(*): all methods which starts with set word and (*) denotes method having only one argument.

SpringBootAopBasicApplicationTests.java

Test case to test functionality.

Run the test case and here we go.

Output

Here first 3 lines is for first setter method (setId) and rest of 3 are for another setter method (setName), here you can see that it intercept setter methods as we defined using @Before annotation.

Share current post by copy: https://goo.gl/8rjkWI
Happy Learning!
:)

Thanks,
Yogesh P