공통된 부가 기능(로깅, 보안 등)을 핵심 기능에서 분리
핵심 기능과 부가 기능을 분리하여 유지보수성을 높이고
코드의 중복을 줄이고 모듈화

보안 인증/권한 관리: 특정 서비스에 접근할 때, 사용자가 올바른 인증을 받았는지 또는 권한이 있는지를 확인하는 기능 구현
특정 메서드에 대해 Pointcut을 정의하는 방식
메서드의 접근 제어자, 리턴 타입, 클래스 이름, 메서드 이름, 매개변수 조건으로 사용
execution(public * com.web.AdminUser.useApp(..))
public : 메서드의 접근 제어자
* : 리턴 타입
com.web.AdminUser : 클래스의 전체 경로
useApp(..) : 메서드 이름과 매개변수(..)
특정한 클래스나 패키지 기준으로 타입 기반으로 Pointcut 지정
within(com.web.*)
@Aspect
public class UserAspect {
//메서드 @Pointcut, @Before`, `@AfterReturning`, `@AfterThrowing`, `@After`, `@Around`
}
@Pointcut("execution(* com.web.hw..*.*(..))")
public void myPointCut() {}
@Before("myPointCut()")
public void before() {
System.out.println("애플리케이션을 시작합니다.");
}
@AfterReturning("myPointCut()")
public void afterReturn() {
System.out.println("애플리케이션 사용을 끝냅니다.");
}
@AfterThrowing("myPointCut()")
public void afterThrow() {
System.out.println("애플리케이션에 문제가 생겨 점검합니다.");
}
@After("myPointCut()")
public void after() {
System.out.println("애플리케이션을 상태와 관련없이 종료합니다.");
}
@Around("myPointCut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("메서드 실행 전");
Object result = joinPoint.proceed(); // 실제 메서드 실행
System.out.println("메서드 실행 후");
return result;
}