Logging method arguments using a before advice

A before advice executes some extra code before the execution of the target method. In this recipe, we will log the arguments of the target method.

Getting ready

We will use the aspect class defined in the Creating a Spring AOP aspect class recipe.

How to do it…

Here are the steps for logging the methods' arguments using a before advice:

  1. In your aspect class, create an advice method annotated with @Before and take JoinPoint as an argument:
    @Before("execution(* com.spring_cookbook.controllers.*.*(..))")
    public void logArguments(JoinPoint joinPoint) {
    ...
    }
  2. In that method, get the list of arguments of the target method:
    Object[] arguments = joinPoint.getArgs();
  3. Log the list of arguments preceded by the target ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.