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:
- In your aspect class, create an advice method annotated with
@Before
and takeJoinPoint
as an argument:@Before("execution(* com.spring_cookbook.controllers.*.*(..))") public void logArguments(JoinPoint joinPoint) { ... }
- In that method, get the list of arguments of the target method:
Object[] arguments = joinPoint.getArgs();
- 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 + "(...