@Around marks advice that wraps selected method calls. Its pointcut expression tells Spring which calls to intercept; the advice can run logic around the intercepted method. section 6.2.1
A ProceedingJoinPoint gives the advice control over what happens next:
- Call proceed() to invoke the intercepted method; the advice can run before and after that call. section 6.2.1
- Skip proceed() to prevent the intercepted method from running; the advice executes instead. section 6.2.1
Logging around a service call
An advice targeting methods in the services package logs before and after proceed(). A call to CommentService.publishComment then produces the advice logs around the service’s own log. section 6.2.1
Listing 6.9 Implementing the aspect logic
@Aspect
public class LoggingAspect {
private final Logger logger =
Logger.getLogger(LoggingAspect.class.getName());
@Around("execution(* com.example.services.*.*(..))")
public void log(ProceedingJoinPoint joinPoint)
throws Throwable {
logger.info("Method will execute"); #A
joinPoint.proceed(); #B
logger.info("Method executed"); #C
}
}See the complete LoggingAspect advice and its before, proceed, and after sequence. section 6.2.1
@Around is the most powerful of Spring’s five advice annotations, but aspects are best used when they genuinely help rather than being applied everywhere. section 6.5
