Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[스프링AOP 포인트컷] 매개변수 전달 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

[스프링AOP 포인트컷] 매개변수 전달

Seung__ 2022. 1. 7. 23:13
  • 다음포인트컷들은 표현식을 사용해서 어드바이스에 매개변수를 전달할 수 있다.
    this, target, args,@target, @within, @annotation, @args
@Before("allMember() && args(arg,..)")
public void logArgs3(String arg) {
	log.info("[logArgs3] arg={}", arg);
}

 

  • 포인트컷의 이름과 매개변수의 이름을 맞추어야 한다. 여기서는 arg 로 맞추었다.
  • 추가로 타입이 메서드에 지정한 타입으로 제한된다. (당연...)
  • 여기서는 메서드의 타입이 String 으로 되어 있기 때문에 다음과 같이 정의되는 것으로 이해하면 된다.
    args(arg,..) --> args(String,..)

 

1. Test


@Slf4j
@Import(ParameterTest.ParameterAspect.class)
@SpringBootTest
public class ParameterTest {

    @Autowired
    MemberService memberService;

    @Test
    void success(){
        log.info("memberService Proxy={}", memberService.getClass());
        memberService.hello("helloA");
    }

    @Slf4j
    @Aspect
    static class ParameterAspect{

        @Pointcut("execution(* hello.aop.member..*.*(..))")
        private void allMember(){};

        @Around("allMember()")
        public Object logArgs1(ProceedingJoinPoint joinPoint) throws Throwable {
            Object arg1 = joinPoint.getArgs()[0];
            log.info("[logArgs1]{}, arg={}", joinPoint.getSignature(), arg1);
            return joinPoint.proceed();
        }

        @Around("allMember() && args(arg, ..)")
        public Object logArgs2(ProceedingJoinPoint joinPoint, Object arg) throws Throwable {
            log.info("[logArgs2]{}, arg={}", joinPoint.getSignature(), arg);
            return joinPoint.proceed();
        }

        //joinPoint 안쓰면 안받아도됨.
       @Before("allMember() && args(arg, ..)")
       public void logArgs3(String arg){
           log.info("[logArgs3] arg={}", arg);
           //종료후 proceed()호출됨.
       }

       //this: 스프링 컨테이너에 올라간 프록시
       @Before("allMember() && this(obj)")
        public void thisArgs(JoinPoint joinPoint, MemberService obj){
           log.info("[this]{}, obj={}", joinPoint.getSignature(),obj.getClass());
       }

       //target: 프록시가 호출한 실제 대상
        @Before("allMember() && target(obj)")
        public void targetArgs(JoinPoint joinPoint, MemberService obj){
            log.info("[target]{}, obj={}", joinPoint.getSignature(),obj.getClass());
        }

        @Before("allMember() && @target(annotation)")
        public void atTarget(JoinPoint joinPoint, ClassAop annotation){
            log.info("[@within]{}, obj={}", joinPoint.getSignature(),annotation);
        }


        @Before("allMember() && @within(annotation)")
        public void atWithin(JoinPoint joinPoint, ClassAop annotation){
            log.info("[@target]{}, obj={}", joinPoint.getSignature(),annotation);
        }

        @Before("allMember() && @annotation(annotation)")
        public void atAnnotation(JoinPoint joinPoint, MethodAop annotation){
            log.info("[@annotation]{}, annotationValue={}", joinPoint.getSignature(),annotation.value());
        }

    }

}
  • logArgs1 : joinPoint.getArgs()[0] 와 같이 매개변수를 전달 받는다.
  • logArgs2 : args(arg,..) 와 같이 매개변수를 전달 받는다.
  • logArgs3 : @Before 를 사용한 축약 버전이다. 추가로 타입을 String 으로 제한했다.
  • this : 빈(프록시) 객체를 전달 받는다.
  • target : 실제 대상 객체를 전달 받는다.
  • @target , @within : 타입의 애노테이션을 전달 받는다.
  • @annotation : 메서드의 애노테이션을 전달 받는다.
     여기서는 annotation.value() 로 해당 애노테이션의 값을 출력하는 모습을 확인할 수 있다.
[logArgs1]String hello.aop.member.MemberServiceImpl.hello(String), arg=helloA
[logArgs2]String hello.aop.member.MemberServiceImpl.hello(String), arg=helloA
[@annotation]String hello.aop.member.MemberServiceImpl.hello(String), annotationValue=test value
[@within]String hello.aop.member.MemberServiceImpl.hello(String), obj=@hello.aop.member.annotation.ClassAop()
[@target]String hello.aop.member.MemberServiceImpl.hello(String), obj=@hello.aop.member.annotation.ClassAop()
[logArgs3] arg=helloA
[target]String hello.aop.member.MemberServiceImpl.hello(String), obj=class hello.aop.member.MemberServiceImpl
[this]String hello.aop.member.MemberServiceImpl.hello(String), obj=class hello.aop.member.MemberServiceImpl$$EnhancerBySpringCGLIB$$370de965

 

 

2. GitHub: 220107 매개변수 전달


 

 

GitHub - bsh6463/SpringAOP

Contribute to bsh6463/SpringAOP development by creating an account on GitHub.

github.com

 

Comments