spring core aop : spring aop
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.example.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.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* 어드바이스 순서
|
||||
*/
|
||||
@Slf4j
|
||||
public class AspectV5Order {
|
||||
|
||||
@Aspect
|
||||
@Order(2)
|
||||
public static class LogAspect {
|
||||
@Around("com.example.aop.order.aop.Pointcuts.allOrder()")
|
||||
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
log.info("[log] {}", joinPoint.getSignature());
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
@Aspect
|
||||
@Order(1)
|
||||
public static class TxAspect {
|
||||
// com.example.aop.order 패키지와 하위 패키지 이면서 클래스 이름 패턴이 *Service
|
||||
@Around("com.example.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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.example.aop.order.aop;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
|
||||
@Slf4j
|
||||
@Aspect
|
||||
public class AspectV6Advice {
|
||||
|
||||
// com.example.aop.order 패키지와 하위 패키지 이면서 클래스 이름 패턴이 *Service
|
||||
@Around("com.example.aop.order.aop.Pointcuts.orderAndService()")
|
||||
public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
|
||||
try {
|
||||
// @Before
|
||||
log.info("[트랜잭션 시작] {}",joinPoint.getSignature());
|
||||
Object result = joinPoint.proceed();
|
||||
|
||||
// @AfterReturning
|
||||
log.info("[트랜잭션 커밋] {}",joinPoint.getSignature());
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
// @AfterThrowing
|
||||
log.info("[트랜잭션 롤백] {}",joinPoint.getSignature());
|
||||
throw e;
|
||||
} finally {
|
||||
// @After
|
||||
log.info("[리소스 릴리즈] {}",joinPoint.getSignature());
|
||||
}
|
||||
}
|
||||
|
||||
@Before("com.example.aop.order.aop.Pointcuts.orderAndService()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
log.info("[before] {}", joinPoint.getSignature());
|
||||
}
|
||||
|
||||
@AfterReturning(value = "com.example.aop.order.aop.Pointcuts.orderAndService()", returning = "result")
|
||||
public void doReturn(JoinPoint joinPoint, Object result) {
|
||||
log.info("[return] {} return {}", joinPoint.getSignature(), result);
|
||||
}
|
||||
|
||||
@AfterReturning(value = "com.example.aop.order.aop.Pointcuts.allOrder()", returning = "result")
|
||||
public void doReturn2(JoinPoint joinPoint, String result) {
|
||||
log.info("[return2] {} return2 {}", joinPoint.getSignature(), result);
|
||||
}
|
||||
|
||||
@AfterThrowing(value = "com.example.aop.order.aop.Pointcuts.orderAndService()", throwing = "ex")
|
||||
public void doReturn(JoinPoint joinPoint, Exception ex) {
|
||||
log.info("[ex] {} message {}", joinPoint.getSignature(), ex);
|
||||
}
|
||||
|
||||
@After("com.example.aop.order.aop.Pointcuts.orderAndService()")
|
||||
public void doAfter(JoinPoint joinPoint) {
|
||||
log.info("[after] {}", joinPoint.getSignature());
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,7 @@ package com.example.aop;
|
||||
|
||||
import com.example.aop.order.OrderRepository;
|
||||
import com.example.aop.order.OrderService;
|
||||
import com.example.aop.order.aop.AspectV1;
|
||||
import com.example.aop.order.aop.AspectV2;
|
||||
import com.example.aop.order.aop.AspectV3;
|
||||
import com.example.aop.order.aop.AspectV4Pointcut;
|
||||
import com.example.aop.order.aop.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -19,7 +16,9 @@ import org.springframework.context.annotation.Import;
|
||||
//@Import(AspectV1.class)
|
||||
//@Import(AspectV2.class)
|
||||
//@Import(AspectV3.class)
|
||||
@Import(AspectV4Pointcut.class)
|
||||
//@Import(AspectV4Pointcut.class)
|
||||
//@Import({AspectV5Order.LogAspect.class, AspectV5Order.TxAspect.class})
|
||||
@Import(AspectV6Advice.class)
|
||||
public class AopTest {
|
||||
|
||||
@Autowired
|
||||
|
||||
Reference in New Issue
Block a user