본문 바로가기

Spring/Advanced13

스프링 AOP - 실무 주의사항 프록시와 내부 호출 - 문제 대상 객체의 내부에서 메서드 호출이 발생하면 프록시를 거치지 않고 대상 객체를 직접 호출하는 문제가 발생한다. CallServiceV0 @Slf4j @Component public class CallServiceV0 { public void external() { log.info("call external"); internal(); //내부 메서드 호출(this.internal()) } public void internal() { log.info("call internal"); } } CallServiceV0Test @Test void external() { callServiceV0.external(); } @Test void internal() { callServiceV0... 2023. 8. 22.
스프링 AOP - 예제 로그 출력 AOP @Trace @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Trace { } TraceAspect @Slf4j @Aspect public class TraceAspect { @Before("@annotation(hello.aop.exam.annotation.Trace)") public void doTrace(JoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); log.info("[trace] {} args={}", joinPoint.getSignature(), args); } } → @Trace가 붙은 메서드에 어드바이스 적용 재시도 .. 2023. 8. 22.
스프링 AOP - 포인트컷 포인트컷 지시자(Pointcut Designator) 포인트컷 지시자의 종류 execution : 메소드 실행 조인 포인트를 매칭. within : 특정 타입 내의 조인 포인트를 매칭. args : 인자가 주어진 타입의 인스턴스인 조인 포인트 this : 스프링 빈 객체(스프링 AOP 프록시)를 대상으로 하는 조인 포인트 target : Target 객체(스프링 AOP 프록시가 가르키는 실제 대상)를 대상으로 하는 조인 포인트 @target : 실행 객체의 클래스에 주어진 타입의 애노테이션이 있는 조인 포인트 @within : 주어진 애노테이션이 있는 타입 내 조인 포인트 @annotation : 메서드가 주어진 애노테이션을 가지고 있는 조인 포인트를 매칭 @args : 전달된 실제 인수의 런타임 타입이.. 2023. 8. 21.
스프링 AOP 구현 Aspect @Slf4j @Aspect public class AspectV1 { //hello.aop.order 패키지와 하위 패키지 @Around("execution(* hello.aop.order..*(..))") public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[log] {}", joinPoint.getSignature()); //join point 시그니처 return joinPoint.proceed(); } } ※ Pointcut : execution(* hello.aop.order..*(..)) → hello.aop.order 패키지와 그 하위 패키지( .. )를 지정하는 AspectJ 포인트컷 표.. 2023. 8. 21.