From cf003c710c60b0cd728337c7c726d64ea18f0638 Mon Sep 17 00:00:00 2001 From: haerong22 Date: Fri, 26 Nov 2021 00:25:53 +0900 Subject: [PATCH] spring core aop : pointcut - @target, @within, @annotation, @args --- .../aop/pointcut/AtAnnotationTest.java | 36 ++++++++++ .../aop/pointcut/AtTargetAtWithinTest.java | 71 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 spring-core/aop/src/test/java/com/example/aop/pointcut/AtAnnotationTest.java create mode 100644 spring-core/aop/src/test/java/com/example/aop/pointcut/AtTargetAtWithinTest.java diff --git a/spring-core/aop/src/test/java/com/example/aop/pointcut/AtAnnotationTest.java b/spring-core/aop/src/test/java/com/example/aop/pointcut/AtAnnotationTest.java new file mode 100644 index 00000000..2fdaecf9 --- /dev/null +++ b/spring-core/aop/src/test/java/com/example/aop/pointcut/AtAnnotationTest.java @@ -0,0 +1,36 @@ +package com.example.aop.pointcut; + +import com.example.aop.member.MemberService; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; + +@Slf4j +@Import(AtAnnotationTest.AtAnnotationAspect.class) +@SpringBootTest +public class AtAnnotationTest { + + @Autowired + MemberService memberService; + + @Test + void success() { + log.info("memberService Proxy={}", memberService.getClass()); + memberService.hello("helloA"); + } + + @Slf4j + @Aspect + static class AtAnnotationAspect { + @Around("@annotation(com.example.aop.member.annotation.MethodAop)") + public Object doAtAnnotation(ProceedingJoinPoint joinPoint) throws Throwable { + log.info("[@annotation] {}", joinPoint.getSignature()); + return joinPoint.proceed(); + } + } +} diff --git a/spring-core/aop/src/test/java/com/example/aop/pointcut/AtTargetAtWithinTest.java b/spring-core/aop/src/test/java/com/example/aop/pointcut/AtTargetAtWithinTest.java new file mode 100644 index 00000000..2b766a1e --- /dev/null +++ b/spring-core/aop/src/test/java/com/example/aop/pointcut/AtTargetAtWithinTest.java @@ -0,0 +1,71 @@ +package com.example.aop.pointcut; + +import com.example.aop.member.annotation.ClassAop; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; + +@Slf4j +@Import(AtTargetAtWithinTest.Config.class) +@SpringBootTest +public class AtTargetAtWithinTest { + + @Autowired + Child child; + + @Test + void success() { + log.info("child Proxy={}", child.getClass()); + child.childMethod(); + child.parentMethod(); + } + + static class Config { + @Bean + public Parent parent() { + return new Parent(); + } + @Bean + public Child child() { + return new Child(); + } + @Bean + public AtTargetAtWithinAspect atTargetAtWithinAspect() { + return new AtTargetAtWithinAspect(); + } + } + + static class Parent { + public void parentMethod(){} // 부모에만 있는 메서드 + } + + @ClassAop + static class Child extends Parent { + public void childMethod(){} + } + + @Slf4j + @Aspect + static class AtTargetAtWithinAspect { + + // @Target: 인스턴스 기준으로 모든 메서드의 조인 포인트를 선정, 부모 타입의 메서드도 적용 + @Around("execution(* com.example.aop..*(..)) && @target(com.example.aop.member.annotation.ClassAop)") + public Object atTarget(ProceedingJoinPoint joinPoint) throws Throwable { + log.info("[@target] {}", joinPoint.getSignature()); + return joinPoint.proceed(); + } + + // @within: 선택된 클래스 내부에 있는 메서드만 조인 포인트로 선정, 부모타입의 메서드는 적용되지 않음음 + @Around("execution(* com.example.aop..*(..)) && @within(com.example.aop.member.annotation.ClassAop)") + public Object atWithin(ProceedingJoinPoint joinPoint) throws Throwable { + log.info("[@within] {}", joinPoint.getSignature()); + return joinPoint.proceed(); + } + } +}