Performance: Why AOP matters
AOP affects runtime performance by adding method interception layers, impacting CPU usage and response time.
@Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logStart(JoinPoint joinPoint) { System.out.println("Start " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.*.*(..))") public void logEnd(JoinPoint joinPoint) { System.out.println("End " + joinPoint.getSignature().getName()); } }
public class UserService { public void createUser() { System.out.println("Start createUser"); // method logic System.out.println("End createUser"); } public void deleteUser() { System.out.println("Start deleteUser"); // method logic System.out.println("End deleteUser"); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual logging in each method | N/A | N/A | N/A | [X] Bad |
| Centralized logging with AOP | N/A | N/A | N/A | [OK] Good |
myService.doWork() is called?@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.example.MyService.doWork(..))")
public void logBefore() {
System.out.println("Starting work");
}
}
@Service
public class MyService {
public void doWork() {
System.out.println("Doing work");
}
}@Aspect
@Component
public class SecurityAspect {
@Before("execution(* com.example.MyService.secureMethod())")
public void checkSecurity() {
System.out.println("Checking security");
}
}
@Service
public class MyService {
public void secureMethod(String user) {
System.out.println("Secure method for " + user);
}
}Pass the test and this pattern moves unknown