gpt4 book ai didi

java - Spring AOP : @annotation() pointcut does not match type annotation

转载 作者:行者123 更新时间:2023-11-30 01:47:36 27 4
gpt4 key购买 nike

我正在编写一个方面来记录 Controller 中每个 API 调用的请求和响应。我希望能够在类上使用此注释,因此使用 @Target(ElementType.TYPE)

之前我添加了@Target(ElementType.Method),并且我在方法上使用了这个注释,并且工作正常。现在我想将其更改为 @Target(ElementType.TYPE)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReLogger {}
@Aspect
@Component
public class ReLoggerAspect {
public static final Logger log = LoggerFactory.getLogger("ReLoggerAspect");

@PostConstruct
private void postConstruct() {
log.info("ReLoggerAspect Created");
}

@Around("@annotation(ReLogger)")
private Object reqLoggingAspect(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("Request {}",jointPoint.getArgs()[0);
}
}

在类上使用@ReLoggerAspect

@RestController
@RequestMapping(value = "....", produces = { "application/json" })
@ReLogger
public class Samplecontroller {
/** Some logic here**/.....
}

调用 API SampleController 时不会打印请求

最佳答案

您认为@annotation会匹配类型注释的前提是错误的,请参阅(Spring AOP手册](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-pointcuts-designators):

  • @within: Limits matching to join points within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP).

  • @annotation: Limits matching to join points where the subject of the join point (the method being executed in Spring AOP) has the given annotation.

因此,您应该使用@within(completely.qualified.AnnotationType)

关于java - Spring AOP : @annotation() pointcut does not match type annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369389/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com