부가 기능을 하나로 모듈화하여 핵심 비즈니스 로직을 분리하는 프로그래밍 방식
AOP는 핵심 로직과 부가 기능을 분리하여 유지보수성과 재사용성을 높이는 데 매우 유용
implementation 'org.springframework.boot:spring-boot-starter-aop'
@Aspect // 스프링 AOP container에 등록
@Component // 스프링 bean container 등록
@Before: Service 메소드 이전에 AOP 메소드 실행@After: Service 메소드 이후에 AOP 메소드 실행@Before()
public void check1() {
}
@Before("execution( * AopService.*(..) )")
@Before("execution( ① ②.③ ( ④ ) )")
① 리턴 타입
* : 모든 리턴 타입int : int 리턴 타입② 클래스 경로
③ 메소드명
④ 매개변수
(..) : 모든 매개변수에 대해 실행&& args(⑤)@Before("execution( * AopService.*(..) ) && args(⑤) ")
@AfterReturning(value = "execution( * AopService.*(..) )", returning = "result")
public void afterReturn(Object result) {
System.out.println("결과값: " + result);
}
value: 실행 조건절returning: 결과값을 바인딩할 변수명@Around("execution( * AopService.*(..) )")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("메소드명: " + joinPoint.getSignature());
System.out.println("매개변수: " + Arrays.toString(joinPoint.getArgs()));
Object result = joinPoint.proceed(); // Service 메소드 실행
return result; // 결과 반환
}
joinPoint: AOP 대상 객체getSignature(): 실행될 메소드 정보getArgs(): 매개변수 배열proceed(): 실제 Service 메소드 실행 (예외 처리 필수)