Using an after advice to clean up resources
An after advice executes some extra code after the execution of the target method, even if an exception is thrown during its execution. Use this advice to clean up resources by removing a temporary file or closing a database connection. In this recipe, we will just log the target method name.
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 using an after advice:
- In your aspect class, create an advice method annotated with
@After
. Make it takeJoinPoint
as an argument:@After("execution(* com.spring_cookbook.controllers.*.*(..))") public void cleanUp(JoinPoint joinPoint) { ... }
- In that advice method, log the target method name:
String className = joinPoint.getSignature().getDeclaringTypeName(); String methodName = joinPoint.getSignature().getName(); System.out.println("-----" + className + "." + methodName + "() -----"...