728x90
반응형
매개변수 전달
- 포인트컷 표현식을 사용해서 어드바이스에 매개변수 전달할 수 있다.
- this., target, args, @target, @within, @annotation, @args
- 포인트컷 이름과 매개변수의 이름을 맞춰야한다.
- 추가로 타입이 메서드에 지정한 타입으로 제한된다.
1. 가장 기본적인 방식
@Around("allMember()")
public Object logArgs1(ProceedingJoinPoint joinPoint) throws Throwable {
Object arg1 = joinPoint.getArgs()[0];
log.info("[logArgs1]{}, arg={}", joinPoint.getSignature(), arg1);
return joinPoint.proceed();
}

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

3. args 사용(2)
@Before("allMember() && args(arg,..)")
public void logArg3(String arg) {
log.info("[logArg3] arg={}", arg);
}

4. this 와 target 사용
@Before("allMember() && this(obj)")
public void thisArgs(JoinPoint joinPoint, MemberService obj) { // joinPoint 생략 가능
log.info("[this]{}, obj={}", joinPoint.getSignature(), obj.getClass());
}
@Before("allMember() && target(obj)")
public void targetArgs(JoinPoint joinPoint, MemberService obj) { // joinPoint 생략 가능
log.info("[target]{}, obj={}", joinPoint.getSignature(), obj.getClass());
}
- this vs target
- target: 실체 대상 구현체
- this: 스프링 컨테이너에 올라간 객체 → 프록시
- aop 적용할 때 스프링에서 프록시를 만들고 스프링 컨테이너에 올림

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

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

7. @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() 로 해당 애노테이션의 값을 출력하는 모습을 확인할 수 있다.
728x90
반응형
댓글