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 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:


You only need to add below dependency into your pom.



Employee.javapackage com.kode12.vo; import java.io.Serializable; import org.springframework.stereotype.Component; @Component public class Employee implements Serializable { private static final long serialVersionUID = -4795125210761524259L; private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return “Employee [id=” + id + “, name=” + name + “]”; } }


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

package com.kode12.aop;

import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

public class SetterMonitor {

    private Logger logger = Logger.getLogger(getClass().getName());

    @Before("execution(void com.kode12.vo.*.set*(*))")
    public void callSetters(JoinPoint joinPoint) {
        logger.info("Setter Called");
        logger.info("Method Invoked: " + joinPoint.getSignature().getName());
        logger.info("Value Passed: " + joinPoint.getArgs()[0]);

@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.


Test case to test functionality.

package com.kode12;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.kode12.vo.Employee;

public class SpringBootAopBasicApplicationTests {

    Employee employee;

    public void contextLoads() {
        employee.setName("Yogesh P");


Run the test case and here we go.


2017-03-06 16:54:46.547  INFO 1660 --- [           main] com.kode12.aop.SetterMonitor             : Setter Called
2017-03-06 16:54:46.548  INFO 1660 --- [           main] com.kode12.aop.SetterMonitor             : Method Invoked: setId
2017-03-06 16:54:46.549  INFO 1660 --- [           main] com.kode12.aop.SetterMonitor             : Value Passed: 1
2017-03-06 16:54:46.580  INFO 1660 --- [           main] com.kode12.aop.SetterMonitor             : Setter Called
2017-03-06 16:54:46.580  INFO 1660 --- [           main] com.kode12.aop.SetterMonitor             : Method Invoked: setName
2017-03-06 16:54:46.580  INFO 1660 --- [           main] com.kode12.aop.SetterMonitor             : Value Passed: Yogesh P

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!

Yogesh P