@Slf4j
@Aspect
public class Aspect1 {
@Around("execution(* start.aop.order..*(..))")
public Object logging(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("log -> {}", joinPoint.getSignature());
return joinPoint.proceed();
}
}
log -> void start.aop.order.OrderService.orderItem(String)
@Slf4j
@SpringBootTest
@Import(Aspect1.class) < -----Aspect1을 스프링 Bean으로 등록함.
public class AopTest {
.
.
.
}
@Slf4j
@Aspect
public class Aspect2 {
@Pointcut("execution(* start.aop.order..*(..))")
private void allOrder(){} <------------
@Around("allOrder()")
public Object logging(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("log -> {}", joinPoint.getSignature());
return joinPoint.proceed();
}
}
@Slf4j
@Aspect
public class Aspect3 {
@Pointcut("execution(* start.aop.order..*(..))")
private void allOrder(){}
@Pointcut("execution(* *..*Service.*(..))")
private void allService(){}
@Around("allOrder()")
public Object logging(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("log -> {}", joinPoint.getSignature());
return joinPoint.proceed();
}
@Around("allOrder() && allService()")
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());
}
}
}
public class Pointcuts {
@Pointcut("execution(* start.aop.order..*(..))")
public void allOrder(){}
@Pointcut("execution(* *..*Service.*(..))")
public void allService(){}
@Pointcut("allOrder() && allService()")
public void orderAndService(){}
}
@Slf4j
@Aspect
public class Aspect4 {
@Around("start.aop.order.aop.Pointcuts.allOrder()")
public Object logging(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("log -> {}", joinPoint.getSignature());
return joinPoint.proceed();
}
@Around("start.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());
}
}
}
@Slf4j
@Aspect
public class Aspect5 {
@Aspect
@Order(2)
public static class LogAspect {
@Around("start.aop.order.aop.Pointcuts.allOrder()")
public Object logging(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("log -> {}", joinPoint.getSignature());
return joinPoint.proceed();
}
}
@Aspect
@Order(1)
public static class TxAspect {
@Around("start.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());
}
}
}
}
@Order(1)이 먼저 실행되고 그다음 @Order(2)가 실행 된다.
@Import({Aspect5.LogAspect.class, Aspect5.TxAspect.class})
@SpringBootTest
public class AopTest {
}
Import 방법이 조금 다르다.