Book Image


Book Image


Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
About the Author
About the Reviewers

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

    String className = joinPoint.getSignature().getDeclaringTypeName();
    String methodName = joinPoint.getSignature().getName();
    System.out.println("-----" + className + "." + methodName + "() -----");
    for (int i = 0; i < arguments.length...