package org.tukorea.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.tukorea.di.domain.StudentVO;
@Aspect
@Component
public class MemberAspect {
// JoinPoint : 메서드 명과 인수 값 접근 가능
@Before("execution(* read(String))")
public void beforeMethod(JoinPoint jp) {
System.out.println("[BeforeMethod] : 메소드 호출 전");
Signature sig = jp.getSignature();
System.out.println("메소드 이름" + sig.getName());
Object[] obj = jp.getArgs();
System.out.println("인수 값" + obj[0]);
}
@After("execution(* read(String))")
public void afterMethod() {
System.out.println("[AfterMethod] : 메소드 호출 후");
}
@AfterReturning(value = "execution(* read(String))", returning = "member")
public void afterReturningMethod(JoinPoint jp, StudentVO member) {
System.out.println("[afterReturningMethod] : 메소드 호출 후 ");
Signature sig = jp.getSignature();
System.out.println("메소드 이름 :" + sig.getName());
Object[] obj = jp.getArgs();
System.out.println("인수 값 :" +obj[0]);
}
@Around("execution(* read(String))")
public StudentVO aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("[AroundMethod before] : 메소드 호출 전");
// proceed :
// 1. 메서드 수행을 반드시 처리
// 2. 반환 값 반드시 리턴
// 3. before와 after 처리
StudentVO member = (StudentVO) pjp.proceed();
System.out.println("[AroundMethod after] : 메소드 호출 후 ");
return member;
//return null;
}
@AfterThrowing(value="execution(* read(String))", throwing = "ex")
public void afterThrowingMethod(Throwable ex) {
// 메소드 호출이 예외를 내보냈을 때 호출되는 Advice
System.out.println("[AfterThrowingMethod] : 예외 발생 후 ");
System.out.println("exception value = " + ex.toString());
}
}
-
@Aspect-
@Component-
@Before-
@After-
AfterReturning-
Arround-
AfterThrowing-
@Pointcut("execution()")@Before("execution( read(String))"), @After("execution( read(String))")
-
반환형 : void, 매개변수없음
-
pointcut : "execution(* read(String))"
@AfterReturning(value = "execution(* read(String))", returning = "member")
-
메서드의 반환형 정의 : returning = “member”
-
pointcut : "execution(* read(String), )“, returning = "member“)
@Around("execution(* read(String))")
-
메서드의 반환형은 AOP 대상의 메서드 반환형과 호환
-
pointcut : "execution(* read(String))"
@AfterThrowing(value = "execution(* read(String))", throwing = "ex")
-
메서드의 매개변수에 캐치할 예외를 선언
-
pointcut : "execution(* read(String))"
MemberDAOImpl.java
MemberServiceImpl.java