AOP (Aspect-Oriented Programming) — separates cross-cutting concerns (logging, security, transactions) from business logic.
1@Aspect2@Component3public class LoggingAspect {4 @Before("execution(* com.example.service.*.*(..))")5 public void logBefore(JoinPoint joinPoint) {6 System.out.println("Calling: " + joinPoint.getSignature().getName());7 }8 @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")9 public void logAfter(JoinPoint jp, Object result) {10 System.out.println("Returned: " + result);11 }12 @Around("execution(* com.example.service.*.*(..))")13 public Object measureTime(ProceedingJoinPoint pjp) throws Throwable {14 long start = System.currentTimeMillis();15 Object result = pjp.proceed();16 System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms");17 return result;18 }19}
Key concepts: