spring core aop : spring aop
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
public class AspectV1 {
|
||||||
|
|
||||||
|
@Around("execution(* com.example.aop.order..*(..))")
|
||||||
|
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
log.info("[log] {}", joinPoint.getSignature());
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
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.aspectj.lang.annotation.Pointcut;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 포인트컷 분리
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
public class AspectV2 {
|
||||||
|
|
||||||
|
// com.example.aop.order 패키지와 하위 패키지
|
||||||
|
@Pointcut("execution(* com.example.aop.order..*(..))")
|
||||||
|
private void allOrder() {}; // pointcut signature
|
||||||
|
|
||||||
|
@Around("allOrder()")
|
||||||
|
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
log.info("[log] {}", joinPoint.getSignature());
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
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.aspectj.lang.annotation.Pointcut;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 포인트컷 추가
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
public class AspectV3 {
|
||||||
|
|
||||||
|
// com.example.aop.order 패키지와 하위 패키지
|
||||||
|
@Pointcut("execution(* com.example.aop.order..*(..))")
|
||||||
|
private void allOrder() {}; // pointcut signature
|
||||||
|
|
||||||
|
// 클래스 이름 패턴이 *Service
|
||||||
|
@Pointcut("execution(* *..*Service.*(..))")
|
||||||
|
private void allService() {};
|
||||||
|
|
||||||
|
@Around("allOrder()")
|
||||||
|
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
log.info("[log] {}", joinPoint.getSignature());
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
// com.example.aop.order 패키지와 하위 패키지 이면서 클래스 이름 패턴이 *Service
|
||||||
|
@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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
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.aspectj.lang.annotation.Pointcut;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 포인트컷 참조
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
public class AspectV4Pointcut {
|
||||||
|
|
||||||
|
@Around("com.example.aop.order.aop.Pointcuts.allOrder()")
|
||||||
|
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
log.info("[log] {}", joinPoint.getSignature());
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,17 @@
|
|||||||
|
package com.example.aop.order.aop;
|
||||||
|
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
|
||||||
|
public class Pointcuts {
|
||||||
|
|
||||||
|
// com.example.aop.order 패키지와 하위 패키지
|
||||||
|
@Pointcut("execution(* com.example.aop.order..*(..))")
|
||||||
|
public void allOrder() {}; // pointcut signature
|
||||||
|
|
||||||
|
// 클래스 이름 패턴이 *Service
|
||||||
|
@Pointcut("execution(* *..*Service.*(..))")
|
||||||
|
public void allService() {};
|
||||||
|
|
||||||
|
@Pointcut("allOrder() && allService()")
|
||||||
|
public void orderAndService() {};
|
||||||
|
}
|
||||||
@@ -2,15 +2,24 @@ package com.example.aop;
|
|||||||
|
|
||||||
import com.example.aop.order.OrderRepository;
|
import com.example.aop.order.OrderRepository;
|
||||||
import com.example.aop.order.OrderService;
|
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 lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.assertj.core.api.Assertions;
|
import org.assertj.core.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.aop.support.AopUtils;
|
import org.springframework.aop.support.AopUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
//@Import(AspectV1.class)
|
||||||
|
//@Import(AspectV2.class)
|
||||||
|
//@Import(AspectV3.class)
|
||||||
|
@Import(AspectV4Pointcut.class)
|
||||||
public class AopTest {
|
public class AopTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|||||||
Reference in New Issue
Block a user