백엔드/Spring(Boot)
스프링 AOP 포인트 컷 외부 설정
김어찐
2021. 11. 3. 01:54
728x90
pointcut 모아놓은 클래스
외부에서 접근 가능하게 public 설정
package hello.aop.order.aop;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
public class Pointcuts {
// hello.aop.order 패키지와 하위패키지
@Pointcut("execution(* hello.aop.order..*(..))")
public void allOrder(){} //pointcu signature
// 클래스 이름 패턴이 *Service
@Pointcut("execution(* *..*Service.*(..))")
public void allService(){}
@Pointcut("allOrder() && allService()")
public void orderAndService(){}
}
외부 포인트 컷 사용
패키지 명, 클래스 명 , 함수명 다 적어줘야한다.
package hello.aop.order.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Slf4j
@Aspect
public class AspectV4Pointcut {
@Around("hello.aop.order.aop.Pointcuts.allOrder()")
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable{
log.info("[log] {}",joinPoint.getSignature());
return joinPoint.proceed();
}
//hello.aop.order 패키지와 하위 패키지 이면서 클래스 이름 패턴이 *Service
@Around("hello.aop.order.aop.Pointcuts.orderAndService()")
public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
try {
log.info("[트랜잭션 시작] {}", joinPoint.getSignature());
Object result = joinPoint.proceed();
log.info("[트랜잭션 커밋] {}", joinPoint.getSignature());
return result;
} catch (Exception e) {
log.info("[트랜잭션 롤백] {}", joinPoint.getSignature());
throw e;
} finally {
log.info("[리소스 릴리즈] {}", joinPoint.getSignature());
}
}
}
728x90