@Around(value = "@annotation(firstMethod) || @annotation(secondMethod)",
argNames = "joinPoint,firstMethod,secondMethod")
public Object SomethingAnnotated(ProceedingJoinPoint joinPoint,
FirstMethod firstMethod, SecondMethod secondMethod) throws Throwable {
return joinPoint.proceed();
}
Error creating bean with name 'org.springframework.boot.autoconfigure.web.
servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat'
: error at ::0 inconsistent binding
@Around(value = "firstAnnotated(firstMethod) &&
!secondAnnotated(secondMethod)", argNames = "joinPoint,firstMethod,secondMethod")
negation doesn't allow binding
부정형은 바인딩이 불가능..
@Aspect
@Component
@Slf4j
public class SampleAspect {
@Pointcut("@annotation(com.example.aoppractice.annotation.FirstMethod)")
public void firstAnnotated(){}
@Pointcut("@annotation(com.example.aoppractice.annotation.SecondMethod)")
public void secondAnnotated(){}
@Around(value = "firstAnnotated() || secondAnnotated()")
public Object BothAnnotated(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed();
}
}
어플리케이션 부팅까지 성공했다. 이제 문제는 annotation 사용하기
@Around(value = "firstAnnotated() || secondAnnotated()")
public Object BothAnnotated(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
FirstMethod firstMethod = method.getAnnotation(FirstMethod.class);
SecondMethod secondMethod = method.getAnnotation(SecondMethod.class);
log.info(firstMethod.toString());
log.info(secondMethod.toString());
return joinPoint.proceed();
}
@GetMapping("/first")
public void first() {
helloService.first();
}
@GetMapping("/second")
public void second() {
helloService.second();
}
@GetMapping("/both")
public void both() {
helloService.both();
}
이 세가지 API를 통해 실험해봤을 때, both에서는 문제가 발생하지 않았는데 first, second 에서는 toString() 을 호출하며 NullPointerException이 발생했다.
아무래도 OR 조건으로 Around 를 사용하려면 이렇게 직접 getAnnotation()을 호출해야 할 것 같다.
Null값에 대한 방어가 관건이겠다.