🛠️
AOP

AOP Real-World Use Cases

Logging, Auditing, Caching
💡 AOP use cases ek FACTORY ke QUALITY CONTROL SYSTEM jaise hain — HAR PRODUCT (method call) automatically CHECK hota hai (logged, audited, timed), bina PRODUCTION LINE ke DESIGN (business logic) ko MODIFY kiye — quality control ALAG, INDEPENDENT layer hai.

AOP ke SABSE COMMON REAL-WORLD use cases: LOGGING (method entry/exit, execution time), SECURITY (@PreAuthorize internally AOP hai), TRANSACTION MANAGEMENT (@Transactional bhi AOP-based hai), CACHING (@Cacheable — agar cache mein value hai, ACTUAL method call hi SKIP kar do), AUDITING (WHO ne KYA action kiya, kab).

Custom annotations (jaise @Auditable, @RateLimited) banaके, un par AOP advice apply karна ek POWERFUL PATTERN hai — business logic COMPLETELY CLEAN rehta hai, cross-cutting behavior ANNOTATION se DECLARATIVELY add hota hai.

// Custom annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auditable {
    String action();
}

// AOP se automatically audit-log:
@Aspect
@Component
public class AuditAspect {
    @AfterReturning("@annotation(auditable)")
    public void logAudit(JoinPoint jp, Auditable auditable) {
        String user = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("AUDIT: " + user + " performed " + auditable.action());
    }
}

// Business logic — COMPLETELY CLEAN, sirf ek annotation:
@Auditable(action = "DELETE_USER")
public void deleteUser(Long id) {
    userRepository.deleteById(id);
}
🛠️
AOP use cases ek FACTORY ke QUALITY CONTROL SYSTEM jaise hain — HAR PRODUCT (method call) automatically CHECK hota hai (logged, audited, timed), bina PRODUCTION LINE ke DESIGN (business logic) ko MODIFY kiye — quality control ALAG, INDEPENDENT layer hai.
1 / 2
⚡ झट से Recap
  • Logging, security (@PreAuthorize), transactions (@Transactional), caching (@Cacheable)
  • Custom annotations + AOP = declarative, clean business logic
  • Overuse se debugging mushkil ho sakti hai — sirf GENUINE cross-cutting concerns ke liye
इस page में (2 subtopics)

Ek REAL-WORLD example — HAR service method ki EXECUTION TIME log karna, bina HAR method mein manually timing code likhे. Ek AOP aspect ye CENTRALLY handle kar sakta hai, saari service methods ke liye EK SAATH.

@Aspect
@Component
public class PerformanceAspect {
    @Around("execution(* com.example.service.*.*(..))")
    public Object measureTime(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.nanoTime();
        try {
            return pjp.proceed();
        } finally {
            long ms = (System.nanoTime() - start) / 1_000_000;
            System.out.println(pjp.getSignature() + ": " + ms + "ms");
        }
    }
}

Custom annotations (jaise @Auditable) banaके, AOP se combine kiya ja sakta hai — jis method par @Auditable lagाया jaaye, AUTOMATICALLY audit-log entry ban jaaye, bina us method ke ANDAR EXPLICIT logging code likhे.

@Retention(RetentionPolicy.RUNTIME)
public @interface Auditable { String action(); }

@Aspect
@Component
public class AuditAspect {
    @AfterReturning("@annotation(auditable)")
    public void audit(JoinPoint jp, Auditable auditable) {
        System.out.println("AUDIT: " + auditable.action() + " by " + getCurrentUser());
    }
}

// Usage:
@Auditable(action = "DELETE_USER")
public void deleteUser(Long id) { /* ... */ }