예를 들면, 로깅, 트랜잭션 많이 사용되고 있으며 스프링에서 사용되는 어노테이션은 AOP기반이다.
테스트를 위해 설정과 코드를 작성해다.
implementation 'org.springframework.boot:spring-boot-starter-aop'
@Aspect
public class Performance {
@Pointcut("execution(* com.u.aop.board.BoardService.findAll(..))")
public void getBoards(){}
@Pointcut("execution(* com.u.aop.user.UserService.findAll(..))")
public void getUsers(){}
@Around("getBoards() || getUsers()")
public Object calculatePerformance(ProceedingJoinPoint joinPoint) {
Object result = null;
try {
long start = System.currentTimeMillis();
result = joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("수행시간 : " + (end - start));
} catch (Throwable throwable) {
System.out.println("exception! ");
}
return result;
}
}
advice는 @Aspect 클래스에서 사용하는 내부 메소드를 말하며, 애스팩트가 ‘무엇’을 ‘언제’할지를 정의한다.
pointcut은 advice를 적용할 조인포인트를 선별하는 기능을 정의한 모듈이다.
joinpoint는 advice가 적용될 수 있는 위치를 말한다.