8.2 Core Terminology and Practical @Around Logging
1. Summary of AOP Core Terminology
To wield AOP freely, you must completely master the following 5 terminologies.
- Aspect: The main macro module that aggregates scattered Cross-cutting Concerns (such as Logging, Security, and Transaction Management). (Advice + Pointcut)
- Advice: The tangible code chunk defining what common logic will actually execute. (Defines execution timing via
@Around,@Before, etc.) - JoinPoint: The entire spectrum of "possible merge points" within application execution where Advice can intrude and apply. (However, due to technical limitations, Spring AOP exclusively supports 'method execution' points.)
- Pointcut: A sophisticated parsing expression that filters down the countless JoinPoints to precisely target where this specific Advice should apply.
- Target: The ripple destination object bearing the original core business logic where the Advice is actually enforced.
2. Practical Execution Time Logging using @Around
In actual production, the most predominantly used AOP pattern is performance logging, which universally measures the execution processing time of methods falling under specific core package hierarchies.
@Slf4j
@Aspect
@Component
public class PerformanceLoggingAspect {
// Designate all methods recursively under the com.example.service package as the Pointcut
@Around("execution(* com.example.service..*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
// 1. Execute Core Business Logic (Delegate invocation to the Target Proxy method)
Object result = joinPoint.proceed();
// 2. Log forcefully post-execution
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
log.info("[{}] Execution Duration: {}ms", joinPoint.getSignature().toShortString(), executionTime);
return result; // Explicitly returning the original computed result is critical for safe continuation.
}
}
ProceedingJoinPoint: Used strictly and exclusively as a parameter within the@Aroundscope. It supplies the specialproceed()control method, which forcefully pierces the Proxy boundary to proactively drive the original Target method.